I am confused with some of the examples that I have come across with C++11 lambdas. For eg:
#include <iostream>
#include <string>
using namespace std;
int main()
{
cout << []()->string{return "Hello World 1!";}() << endl;
[]{cout << "Hello World 2!" << endl;}();
string result = [](const string& str)->string {return "Hello World " + str;}("2!");
cout << "Result: " << result << endl;
result = [](const string& str){return "Hello World " + str;}("3!");
cout << "Result: " << result << endl;
string s;
[&s](){s = "Hello World 4!";}; // does not work
cout << s << endl;
[&s](){s = "Hello World 4!";}(); // works!
cout << s << endl;
return 0;
}
I am unable to figure out what the parentheses at the end are doing. Are they instantiating, as a constructor, a lambda? Given that the template for a lambda is:
[capture_block](parameters) mutable exception_specification -> return_type {body}
it baffles me that those parentheses are required for those instances to work. Can someone explain what they are why they are required?
They're calling the function object!
In two phases:
A lambda expression evaluates to a function object. Since there's no
operator<<
overload that takes such a function object, you'd get an error if you didn't call it to produce astd::string
.Well, given that a lambda expression is basically an anonymous function, the parentheses at the end do nothing more than just call this function. So
is just equivalent to
This holds in the same way for a lambda with no arguments, like in
which would otherwise (if not called) try to output a lambda expression, which in itself doesn't work. By calling it it just puts out the resulting
std::string
.