getting type from lambda c++11

Hi all,
Is there any way of creating a variable which type is the result of some lambda expression.
something like this doesn't work:
 
decltype([](){return 1;}()) a;


Is there a workaround?
Last edited on
Yes, and no, depending on what you really mean. If you want the type of the lambda itself, then no. Buf, if you just want the return type, it's easy!
1
2
auto x=[](){return 1;};
decltype(x()) a;
Thanks!!
Do you know what is the rational behind not allowing decltype to accept the result of a lambda calculation in the first place?
Last edited on
closed account (o1vk4iN6)
Why not just

 
decltype( 1 ) a;



Also seems a bit counter productive and you'll need two copies of the same lambda function.

Last edited on
Do you know what is the rational behind not allowing decltype to accept the result of a lambda calculation in the first place?

In general, it's impossible to determine the type of a lambda expression without evaluating it in the correct context. So, lamdbas are forbidden in unevaluated contexts, and decltype is one of such contexts.
Yes! The lambda is just a way of evluating a closure, and since you are EVALUATING it to get the closure, it can't be done in an UNEVALUATED context!
Topic archived. No new replies allowed.