C2665 with lambda function and enumeration in Visual 2010, is this a bug or is this normal?

I can get the following code to compile:

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

but not the following:

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

which produces the following compiler error:

 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)' 

Is this error expected or is it an error?

+4
source share
1 answer

This seems like a problem at http://social.msdn.microsoft.com/Forums/en/vclanguage/thread/88f533d8-b7f5-4416-bdcf-b461aeb74178 . As there, this seems to be a bug in the compiler. Apparently, MSVC has some problems with local types in lambdas; see also http://connect.microsoft.com/VisualStudio/feedback/details/675113/lambda-expression-causes-internal-compiler-error#details .

In 5.1.2 Lambda expressions [expr.prim.lambda] there is no language to say that locally defined types cannot be written to lambda.

+6
source

All Articles