我可以得到下面的代码编译:带有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