C++ 11でラムダを覚えた後、私はこれを書いて、出力に混乱しました。この場合のラムダ出力との混乱
auto f1 = []() {
int tmp = 10;
int *tmp_p = &tmp;
return [tmp_p] (int x) {
return *tmp_p + x;
};
}();
auto f2 = []() {
int tmp = 10;
return [&tmp] (int x) {
return tmp + x;
};
}();
cout << f1(5) << endl;
cout << f1(5) << endl;
cout << f2(5) << endl;
cout << f2(5) << endl;
出力は次のとおりです。
15
5772973
2686617
2686617
この背後にある理由は何ですか?
しかし、パラメータ付きのラムダを返してすぐに実行されます。 –
@AdrianJałoszewskiごめんなさい –