以下のように単純なラムダを作成しましたが、期待どおりに動作します (GCC 4.6.4 および 4.7.2 -- demo )。しかし、その後、標準を確認したところ、5.1.2-8では、ラムダ キャプチャでの=
andの使用が明示的に禁止されています。this
... lambda-capture に= であるcapture-defaultが含まれる場合、 lambda-captureにはこれが含まれず、含まれる各識別子の前に & が付きます。...
私は何か間違ったことを読んでいますか?これは実際に許可されていますか? いいえの場合、許可されていない理由がわかりません。また、GCCがそれを許可するのは間違っているということですか?
#include <iostream>
#include <functional>
using namespace std;
struct sample {
int a;
std::function<int()> get_simple(int o) {
return [=,this]() {
return a + o;
};
}
};
int main() {
sample s;
auto f = s.get_simple(5);
s.a = 10;
cout << f() << endl; //prints 15 as expected
}