2012-08-29 168 views
4

我可以得到下面的代碼編譯:帶有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)' 

那是錯誤預見的或這是一個錯誤?

+0

std :: function f = [=,&e](){e = a; }編譯。我認爲重點是全局變量或靜態變量可以在capture子句中不提及它們(按spec)進行訪問。因此,你的問題簡化爲以下內容:根據規範,[&]應該捕獲本地定義的枚舉常量,或者不是? – JohnB

+0

可能相關:http://connect.microsoft.com/VisualStudio/feedback/details/544013/visual-c-failure-to-reference-function-local-enum-constant-from-lambda – JohnB

回答

6

這看起來與http://social.msdn.microsoft.com/Forums/en/vclanguage/thread/88f533d8-b7f5-4416-bdcf-b461aeb74178上的問題完全相同。在那裏,它似乎是編譯器中的一個錯誤。 MSVC在lambdas中似乎有一些本地類型的問題;另請參閱http://connect.microsoft.com/VisualStudio/feedback/details/675113/lambda-expression-causes-internal-compiler-error#details

沒有語言5.1.2 Lambda表達式[expr.prim.lambda]表示本地定義的類型不能在lambda中捕獲。

+2

+1另外,這個bug在VC++ 2012 RTM中修復。 – ildjarn

相關問題