我可以得到下面的代碼編譯:帶有lambda函數的C2665和Visual 2010中的enum,它是一個錯誤還是正常?
enum E {a, b, c};
void f()
{
E e;
std::function<void()> f = [&]() { e = a; };
}
但不是下列之一:
void f()
{
enum E {a, b, c};
E e;
std::function<void()> f = [&]() { e = a; };
}
其發出以下編譯器錯誤:
1>test.cpp(5): error C2665: '`anonymous-namespace'::<lambda1>::<lambda1>' : none of the 2 overloads could convert all the argument types
1> test.cpp(5): could be '`anonymous-namespace'::<lambda1>::(f::E &,f::E &)'
1> while trying to match the argument list '(f::E, f::E)'
那是錯誤預見的或這是一個錯誤?
std :: function f = [=,&e](){e = a; }編譯。我認爲重點是全局變量或靜態變量可以在capture子句中不提及它們(按spec)進行訪問。因此,你的問題簡化爲以下內容:根據規範,[&]應該捕獲本地定義的枚舉常量,或者不是? –
JohnB
可能相關:http://connect.microsoft.com/VisualStudio/feedback/details/544013/visual-c-failure-to-reference-function-local-enum-constant-from-lambda – JohnB