Klammern am Ende eines C ++ 11-Lambda-Ausdrucks
Ich bin verwirrt mit einigen Beispielen, die ich mit C ++ 11 Lambdas gefunden habe. Zum Beispiel:
#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;
}
Ich kann nicht herausfinden, was die Klammern am Ende tun. Instanziieren sie als Konstrukteur ein Lambda? Vorausgesetzt, die Vorlage für ein Lambda ist:
[capture_block](parameters) mutable exception_specification -> return_type {body}
Es verwundert mich, dass diese Klammern erforderlich sind, damit diese Instanzen funktionieren. Kann jemand erklären, was sie sind, warum sie benötigt werden?