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?
You are trying to initialize the variable with the lambda object not with the result of evaluating the lambda:
int i([]() { return 1; }());
// ^^
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With