#include <iostream>
#include <memory>
#include <functional>
using namespace std;
int main() {
// [] 内に変数名を書くと、無名関数外の変数をコピーして持ってこれる
string str = "Hoge";
[str]() {
cout << str << endl;
}();
cout << "-----" << endl;
// そのままだと内容が変更できないが、mutable と記述すると変更できる。
[str]() mutable {
str = "Fuga";
cout << str << endl;
}();
cout << "-----" << endl;
// キャプチャに & をつけると参照キャプチャになる。
// 参照なので元の変数に影響を与えられる。
string strA = "AAA";
string strB = "BBB";
[strA, &strB]() mutable {
cout << "strA:" << strA << endl;
cout << "strB:" << strB << endl;
cout << "-----" << endl;
strA = "ZZZ";
strB = "ZZZ";
// 無名関数内では strA, strB 共に変更が起こる。
cout << "strA:" << strA << endl;
cout << "strB:" << strB << endl;
cout << "-----" << endl;
}();
// 参照キャプチャした strB のみ影響が残る
cout << "strA:" << strA << endl;
cout << "strB:" << strB << endl;
return 0;
}