Thread: C++11 : Lambda functions return value

  1. #1
    Registered User
    Join Date
    Aug 2011
    Location
    Montreal, Quebec, Canada
    Posts
    73

    C++11 : Lambda functions return value

    Hey there. I installed the newest GCC version yesterday to try out the new C++ standard. I have added the command line argument so that it knows to use C++11 and compiled a sample code from CProgramming's webmaster successfully. I wanted to play around a little bit to learn how to use the new tools but it seems I can't make anything compile:
    Code:
    #include <iostream>
    
    using namespace std;
    
    void blah(int a) {
        std::cout << a;
    }
    
    int main()
    {
        auto a = [] { return 1; };
        
        blah(a);
        blah([] { return 1; });
        blah([] -> int { return 1; });
    }
    Code:
    "/usr/bin/make" -f nbproject/Makefile-Debug.mk QMAKE= SUBPROJECTS= .build-conf
    make[1]: entrant dans le répertoire « /cygdrive/c/Users/win7/Documents/NetBeansProjects/CppApplication_1 »
    "/usr/bin/make"  -f nbproject/Makefile-Debug.mk dist/Debug/Cygwin_4.x-Windows/cppapplication_1.exe
    make[2]: entrant dans le répertoire « /cygdrive/c/Users/win7/Documents/NetBeansProjects/CppApplication_1 »
    mkdir -p build/Debug/Cygwin_4.x-Windows
    rm -f build/Debug/Cygwin_4.x-Windows/main.o.d
    g++-4.exe -std=c++0x -Wall -Wextra   -c -g -MMD -MP -MF build/Debug/Cygwin_4.x-Windows/main.o.d -o build/Debug/Cygwin_4.x-Windows/main.o main.cpp
    main.cpp: In function ‘int main()’:
    main.cpp:13:11: erreur: invalid conversion from ‘int (*)()’ to ‘int’
    main.cpp:13:11: erreur:   initializing argument 1 of ‘void blah(int)’
    main.cpp:14:26: erreur: invalid conversion from ‘int (*)()’ to ‘int’
    main.cpp:14:26: erreur:   initializing argument 1 of ‘void blah(int)’
    main.cpp:15:36: erreur: invalid conversion from ‘int (*)()’ to ‘int’
    main.cpp:15:36: erreur:   initializing argument 1 of ‘void blah(int)’
    make[2]: quittant le répertoire « /cygdrive/c/Users/win7/Documents/NetBeansProjects/CppApplication_1 »
    make[1]: quittant le répertoire « /cygdrive/c/Users/win7/Documents/NetBeansProjects/CppApplication_1 »
    make[2]: *** [build/Debug/Cygwin_4.x-Windows/main.o] Erreur 1
    make[1]: *** [.build-conf] Erreur 2
    make: *** [.build-impl] Erreur 2
    
    BUILD FAILED (exit value 2, total time: 2s)
    Obviously this is all the same syntax error, but I do not understand what my error is. From what I have seen online it seems that this is the way to do this :\

  2. #2
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    Code:
    auto a = [] { return 1; };
    Needs to be
    Code:
    auto a = []() { return 1; };
    Code:
    blah(a);
    Since blah takes an int you need to use a as a function:
    Code:
    blah(a());
    Now to have a function take a lambda function as an argument you need to use std::function:
    Code:
    #include <iostream>
    #include <functional>
    
    int foo(std::function<int()> func)
    {
        return func();
    }
    
    int main()
    {
        auto f = []() { return 1; };
        std::cout << foo(f) << std::endl;
    }
    Note the template argument to std::function, it specifies the returntype and the argument list types. If the lambda function should take an int as an argument we modify it the std::function declaration as such:
    Code:
    #include <iostream>
    #include <functional>
    
    int foo(std::function<int(int)> func)
    {
        return func(1);
    }
    
    int main()
    {
        auto f = [](int a) { return 2*a; };
        std::cout << foo(f) << std::endl;
    }
    Last edited by Shakti; 09-14-2011 at 08:32 AM.

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You can use templates, too, which might be preferred since std::function has some overhead.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Aug 2011
    Location
    Montreal, Quebec, Canada
    Posts
    73
    It seems that I hadn't been reading the tutorial through. I modified my code to the following and it works fine:

    Code:
    #include <iostream>
    #include <functional>
    
    void blah(std::function<int(void)> func) {
        std::cout << func();
    }
    
    int main()
    {
        auto a = []()->int { return 1; };
        blah(a);
        blah([] { return 1; });
        blah([] () -> int { return 1; });
    }

  5. #5
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Quote Originally Posted by Elysia View Post
    You can use templates, too, which might be preferred since std::function has some overhead.
    And you might prefer std::function, too, because it allows you to declare the function in one place and define it somewhere else easily. Unlike template versions which are implemented in h files.

  6. #6
    Registered User
    Join Date
    Aug 2011
    Location
    Montreal, Quebec, Canada
    Posts
    73
    This example was strictly designed for me to try the new Lambda functions feature so templates are, of course, not relevant at all unless there is some point I am missing.

  7. #7
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    One way to give another function a lambda function is through template parameters. I believe the tutorial covers this.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Lambda as argument to functor
    By StainedBlue in forum C++ Programming
    Replies: 1
    Last Post: 07-02-2010, 01:59 PM
  2. c++ equivalent of c# delegate/lambda
    By duck_lee in forum C++ Programming
    Replies: 1
    Last Post: 05-03-2010, 10:56 AM
  3. Lambda functions in C?
    By black0ut in forum C Programming
    Replies: 2
    Last Post: 08-12-2009, 01:26 PM
  4. boost lambda expression
    By pheres in forum C++ Programming
    Replies: 10
    Last Post: 04-26-2007, 05:25 AM
  5. functions return value
    By threahdead in forum C Programming
    Replies: 3
    Last Post: 01-01-2003, 07:04 PM