Visual C++ 2010 compiler bugs

Back to Blog

Here are some of the bugs I have found in the Visual C++ 2010 compiler.

Enumeration capture in a lambda

The following code shows three different errors, depending on the chosen capture mode, which are emitted when trying to capture an enumeration constant into a lamdba function.

template <int N>
class B {};

int main()
{
    enum {M = 2};

    // error C3493: 'M' cannot be implicitly captured because no default 
    // capture mode has been specified

    auto h = []() {B<M> b;}; 

    // error C2440: '<function-style-cast>' : cannot convert from '' to 
    // '`anonymous-namespace'::<lambda0>'. No constructor could take the 
    // source type, or constructor overload resolution was ambiguous

    //auto h = [&]() {B<M> b;}; 

    // error C2975: 'N' : invalid template argument for 'B', expected 
    // compile-time constant expression

    //auto h = [=]() {B<M> b;}; 

    return 0;
}

Internal compiler error

The following code snippet gives an internal compiler error in Visual Studio 2010 SP1. The code makes use of C++11 features.

template<typename N>
class A {};

template <int N>
class B {};

template <int N>
void f()
{
    auto h = []() {B<N> b;};
}

int main()
{
    f<2>();
    return 0;
}

Nested lambdas and outer types

Referring to outer types from nested lambdas causes compiler errors. There are similar problems with capture variables also.

int main()
{
    typedef int C;

    auto f = [&]() 
    {
        auto g = [&]() {C b;};
    }; 

    return 0;
}