locked
lambda expression problems RRS feed

  • Question

  • I'm using a lambda expression to specialize a class that I don't want to sub-class, but I'm having difficulty in compiling certain expressions, and the error messages are not useful. The code that reproduces the compilation problem is the following:

    #include <iostream>
    #include <functional>
    
    using namespace std;
    
    class M
    {
    public:
        function<int (int r, int c)> fun;
    
        M()
        {
            fun = [=] (int r, int c) -> int
            {
                return r * 2 + c;
            };
        }
    };
    
    
    void foo(M * A)
    {
        int test1 = A->fun(1,1);
        cout << test1 << "\n";
    
        auto f1 = [=]()
        {
            int test = A->fun(2,2);
            cout << test << "\n";
        };
        f1();
    
        int * a = (int*)malloc(sizeof(int));
        auto f2 =
            [=] ()
            {
                *a = 2;
            };
        f2();
        cout << *a << "\n";
    
        //auto f3 =
        //    [=] ()
        //    {
        //        int test = A->fun(3,3);
        //        cout << test << "\n";
        //        *a = test;
        //    };
        //f3();
    }
    
    
    int main()
    {
        M * A = new M();
        foo(A);
        return 0;
    }
    

    The code as it is shown compiles and works fine.  The problem occurs when I try to uncomment the block of code defining f3 and making the call to it.

    1>ClCompile:
    1>  Source.cpp
    1>c:\users\ken\documents\visual studio 11\projects\consoleapplication8\consoleapplication8\source.cpp(48): error C2371: '_A' : redefinition; different basic types
    1>          c:\users\ken\documents\visual studio 11\projects\consoleapplication8\consoleapplication8\source.cpp(48) : see declaration of '_A'
    1>c:\users\ken\documents\visual studio 11\projects\consoleapplication8\consoleapplication8\source.cpp(48): error C2661: 'foo::<lambda_4b1774fd83e66fa4519f32071f6f1dd4>::<lambda_4b1774fd83e66fa4519f32071f6f1dd4>' : no overloaded function takes 2 arguments
    1>c:\users\ken\documents\visual studio 11\projects\consoleapplication8\consoleapplication8\source.cpp(49): error C3536: 'f3': cannot be used before it is initialized
    1>c:\users\ken\documents\visual studio 11\projects\consoleapplication8\consoleapplication8\source.cpp(49): error C2064: term does not evaluate to a function taking 0 arguments
    1>c:\users\ken\documents\visual studio 11\projects\consoleapplication8\consoleapplication8\source.cpp(48): error C2440: 'initializing' : cannot convert from 'M *const ' to 'int *'
    1>          Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
    1>c:\users\ken\documents\visual studio 11\projects\consoleapplication8\consoleapplication8\source.cpp(48): error C2439: 'foo::<lambda_4b1774fd83e66fa4519f32071f6f1dd4>::a' : member could not be initialized
    1>          c:\users\ken\documents\visual studio 11\projects\consoleapplication8\consoleapplication8\source.cpp(48) : see declaration of 'foo::<lambda_4b1774fd83e66fa4519f32071f6f1dd4>::a'
    

    This code illustrates the problem because in the definition of f1 and f2, those lambda expressions and calls work fine, but when combined, the expression does not compile.

    I'm using Visual Studio 11 C++ Beta on Windows 7 x64. I haven't tried this yet on the just-released Release Preview.

    I can implement this in several other ways, but I'd like to use lambda expressions.

    What is the solution?

    Ken Domino

    Saturday, June 2, 2012 4:54 PM

Answers

  • Ken Domino wrote:

    The code as it is shown compiles and works fine. The problem occurs  when I try to uncomment the block of code defining f3 and
    making the call to it.

    Looks like a bug to me. Consider filing it at

    https://connect.microsoft.com/VisualStudio


    Igor Tandetnik

    • Marked as answer by Ken Domino Sunday, June 3, 2012 12:28 PM
    Saturday, June 2, 2012 7:32 PM

All replies

  • It looks like my use of short, sweet, simple, one letter variables, like "a" in function foo() is haraam. If I rename the variable to "x", then it compiles and runs fine.  I'm not sure why this is a problem.  Maybe a compiler guy can explain why this is a problem?

    Ken

    Saturday, June 2, 2012 5:37 PM
  • Ken Domino wrote:

    The code as it is shown compiles and works fine. The problem occurs  when I try to uncomment the block of code defining f3 and
    making the call to it.

    Looks like a bug to me. Consider filing it at

    https://connect.microsoft.com/VisualStudio


    Igor Tandetnik

    • Marked as answer by Ken Domino Sunday, June 3, 2012 12:28 PM
    Saturday, June 2, 2012 7:32 PM