Круглые скобки в конце лямбда-выражения C ++ 11
Меня смущают некоторые примеры, которые я встречал с лямбдами C ++ 11. Например:
#include
#include
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;
}