Possible Duplicate:
What is a lambda expression in C++11?
I found this expression in C++ (one of the most exciting features of C++11):
int i = ([](int j) { return 5 + j; })(6);
Why I get the 11? Please explain this expression.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
[](int j) { return 5 + j; }is a lambda that takes anintas an argument and calls itj. It adds 5 to this argument and returns it. The(6)after the expression invokes the lambda immediately, so you’re adding 6 and 5 together.It’s roughly equivalent to this code:
Except, of course, that it does not create a named function. A smart compiler will probably inline the lambda and do constant folding, resulting in a simple reduction to
int i = 11;.