2012-08-29 166 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

相关问题