
----- Original Message -----
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.
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? } main() { execute_me<decltype([](int i) {return i * 100;})>(5); } Thanks for the ideas! Augustus