Boost logo

Boost :

Subject: Re: [boost] Using C++11 lambdas to replace Phoenix in MPL expressions
From: Michel Morin (mimomorin_at_[hidden])
Date: 2012-03-24 23:51:18

Augustus Saunders wrote:
> Lambdas have no default
> constructor, which makes sense, as there would be no way to deal with the capture variables.
> So I'm wondering if anybody can shed some light on manually instantiating lambdas.  I would
> think there is a way (possibly shady) to do this, at least for lambdas with no capture.

For lambdas with no capture, the following code might work:

    #include <iostream>

    int main (int argc, char* argv[])
        auto x = [](int i){ std::cout << i << std::endl; };


        return 0;

But, I strongly discourage its use.

Instead, I recommend wrapping lambdas into boost::optional and
constructing with in-place factories:

    #include <boost/optional.hpp>

    int main (int argc, char* argv[])
        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

        return 0;


Boost list run by bdawes at, gregod at, cpdaniel at, john at