Search code examples
c++c++11lambdavisual-studio-2013

c++ lambda can't convert to int


I attempting to use a nested lambda as follows:

int main() {
    auto x=[](int a){
        return [a](int b){
            return a+b;
        };
    };

    int xx = x(1)(2);
    
    (void)xx;
    return 0;
}

However, the vs2013 compiler yields an error (C++11 language level):

[...] can't convert to int.

What is wrong?


Solution

  • Your code snippet is well-formed, and this is a compiler limitation or bug in the MSVC C++ compiler version used in VS2013. Note that VS2013 only claimed partial C++11 support, as per e.g. What's New for Visual C++ in Visual Studio 2013:

    Improved ISO C/C++ Standards Support

    Compiler

    Supports these ISO C++11 language features:

    • [...]