Nawiasy na końcu wyrażenia lambda C ++ 11
Jestem pomylony z niektórymi przykładami, z którymi zetknąłem się z C ++ 11 lambdami. Na przykład:
#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;
}
Nie jestem w stanie zrozumieć, co robią nawiasy na końcu. Czy tworzą instancję, jako konstruktor, lambdę? Biorąc pod uwagę, że szablon lambda to:
[capture_block](parameters) mutable exception_specification -> return_type {body}
zaskakuje mnie, że te nawiasy są wymagane do działania tych instancji. Czy ktoś może wyjaśnić, dlaczego są wymagane?