我可以得到以下代码进行编译:
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)'
该错误是可预期的还是错误?
最佳答案
这似乎与 http://social.msdn.microsoft.com/Forums/en/vclanguage/thread/88f533d8-b7f5-4416-bdcf-b461aeb74178 的问题相同。在那里,它似乎是编译器中的一个错误。 MSVC 似乎对 lambda 中的本地类型存在一些问题;另见 http://connect.microsoft.com/VisualStudio/feedback/details/675113/lambda-expression-causes-internal-compiler-error#details 。
5.1.2 Lambda 表达式 [expr.prim.lambda] 中没有语言说不能在 lambda 中捕获本地定义的类型。
关于c++ - Visual 2010 中带有 lambda 函数和枚举的 C2665,是错误还是正常?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/12177230/