$include_dir="/home/hyper-archives/boost/include"; include("$include_dir/msg-header.inc") ?>
Subject: Re: [boost] Using C++11 lambdas to replace Phoenix in MPL expressions
From: Michel Morin (mimomorin_at_[hidden])
Date: 2012-03-25 02:13:38
Augustus Saunders wrote:
>> auto x = [](int i){ std::cout << i << std::endl; };
>> (*static_cast<decltype(x)*>(0))(5);
>
> Right, since without a capture, the implicit this pointer passed
> to the operator() of the anonymous class is never dereferenced
> and so just static_casting 0 or null won't actually cause an error.
Unless "Core issue 232" is not resolved,
http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#232
I think this results in undefined behavior.
>>>Instead, I recommend wrapping lambdas into boost::optional and
>>>constructing with in-place factories:
>>>
>>> auto x = [](int i){ return i * 100; }; // Store type and value in x
>>>
>>> boost::optional<decltype(x)> y; // Get type of x and default construct y
>>> y = boost::in_place(x); // Get value of x and lazily construct lambda
>
>
> I'm not sure I follow here. Let me make a more concrete example
> and maybe you can fill in the blank:
>
> template <typename LAMBDA>
> int execute_me(int input)
> {
> return (*static_cast<LAMBDA*>(0))(input);
>
> // ok now same thing using optional?
> boost::optional<LAMBDA> y;
> return y(input); // ?? obviously not but what?
> }
The approach with boost::optional cannot do this.
In-place factories need a lambda object itself.
So maybe you're not interested in this approach.
(But, please note that we cannot know the type of lambda expression
without actually constructing lambda objects.)
Regards,
Michel