Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Initializing a variable by a lambda expression?

I am trying to initialize a variable using a lambda expression. I havn't heard if this is possible or not, so is this possible? For example:

int i([]() { return 1; });

which returns

error C2440: 'initializing' : cannot convert from 'wmain::<lambda_b35514739a4854f3d329a617eabe58c1>' to 'int'

Is this operation possible, and my syntax is merely wrong?

like image 730
KaiserJohaan Avatar asked Jul 29 '13 18:07

KaiserJohaan


2 Answers

You are trying to initialize the variable with the lambda object not with the result of evaluating the lambda:

int i([]() { return 1; }());
//                      ^^
like image 58
David Rodríguez - dribeas Avatar answered Oct 06 '22 19:10

David Rodríguez - dribeas


You need to call the lambda:

int i( []() { return 1; }() );
                      // ^^

The lambda itself is an expression that yiels a a prvalue temporary called a closure object. These are not convertible to int.

like image 20
jrok Avatar answered Oct 06 '22 18:10

jrok