在Visual 2010中使用lambda函数和枚举的C2665,它是一个bug还是正常的?

Ben*_*oit 4 c++ lambda visual-studio-2010 visual-c++ c++11

我可以得到以下代码来编译:

enum E {a, b, c};
void f()
{
    E e;
    std::function<void()> f = [&]() { e = a; };
} 
Run Code Online (Sandbox Code Playgroud)

但不是以下一个:

void f()
{
    enum E {a, b, c};
    E e;
    std::function<void()> f = [&]() { e = a; };
} 
Run Code Online (Sandbox Code Playgroud)

发出以下编译器错误:

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)'
Run Code Online (Sandbox Code Playgroud)

这个错误是可以预料的还是一个错误?

eca*_*mur 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中捕获本地定义的类型.

  • +1此外,VC++ 2012 RTM中修复了此错误. (2认同)