getting type from lambda c++11

Aug 1, 2012 at 5:35pm
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 Aug 1, 2012 at 5:35pm
Aug 1, 2012 at 5:45pm
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;
Aug 1, 2012 at 5:46pm
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 Aug 1, 2012 at 5:48pm
Aug 1, 2012 at 5:49pm
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 Aug 1, 2012 at 5:58pm
Aug 1, 2012 at 9:51pm
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.
Aug 2, 2012 at 12:19pm
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.