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 one:
void f()
{
enum E {a, b, c};
E e;
std::function<void()> f = [&]() { e = a; };
}
which issues 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 that error expectable or is it a bug?
This appears identical to the issue at http://social.msdn.microsoft.com/Forums/en/vclanguage/thread/88f533d8-b7f5-4416-bdcf-b461aeb74178. As there, it appears to be a bug in the compiler. MSVC appears to have a few issues with local types in lambdas; see also http://connect.microsoft.com/VisualStudio/feedback/details/675113/lambda-expression-causes-internal-compiler-error#details.
There is no language in 5.1.2 Lambda expressions [expr.prim.lambda] to say that locally-defined types cannot be captured in a lambda.