Type of lambda function returning a lambda function...

Discussion in 'C++' started by Haochen Xie, Mar 16, 2013.

  1. Haochen Xie

    Haochen Xie Guest

    .... I'm trying to declare a lambda function returning a lambda function, and, unfortunately, I could not use auto key word since it would be the return type of my function. The lambda function looks like this:

    typedef std::function<void(string)> Func;
    vector<pair> set;
    auto lambda = [&set] (string cmd, Func func) {
    set.push_back(pair(cmd, func));
    return lambda;
    };
    return lambda;

    I try to use std::function template to express that, but.. As you see, an infinite recursion occurs...

    So how could I do that? Any one have any idea?

    BTW, the above code compiles without the last returning line. So I CAN have a lambda function like that.

    ---------------------
    Posted by Haochen Xie
    Haochen Xie, Mar 16, 2013
    #1
    1. Advertising

  2. Haochen Xieæ–¼ 2013å¹´3月17日星期日UTC+8上åˆ1時45分42秒寫é“:
    > ... I'm trying to declare a lambda function returning a lambda function, and, unfortunately, I could not use auto key word since it would be the return type of my function. The lambda function looks like this:
    >
    >
    >
    > typedef std::function Func;
    >
    > vector set;
    >
    > auto lambda = [&set] (string cmd, Func func) {
    >
    > set.push_back(pair(cmd, func));
    >
    > return lambda;
    >
    > };
    >
    > return lambda;
    >
    >
    >
    > I try to use std::function template to express that, but.. As you see, aninfinite recursion occurs...
    >
    >
    >
    > So how could I do that? Any one have any idea?
    >
    >
    >
    > BTW, the above code compiles without the last returning line. So I CAN have a lambda function like that.
    >
    >
    >
    > ---------------------
    >
    > Posted by Haochen Xie


    In C/C++ to implement a function which could map an input
    function into another in a 32/64 bit OS is different but similar.

    What really matters is the type of the functions in the domain and
    the co-domain under implementations.

    input output
    1. static fixed compiled codes 1,2,3
    2. JIT compiled byte codes of a VM 1,2,3
    3. JIT compiled machine codes 1,2,3

    Now there are 9 possibilities in different cases.

    I'll suggest you learn the decorator part in python first.
    88888 Dihedral, Mar 16, 2013
    #2
    1. Advertising

  3. Haochen Xie

    Haochen Xie Guest

    On Saturday, March 16, 2013 11:17:43 AM UTC-7, 88888 Dihedral wrote:
    >
    > In C/C++ to implement a function which could map an input
    > function into another in a 32/64 bit OS is different but similar.
    >
    > What really matters is the type of the functions in the domain and
    > the co-domain under implementations.
    >


    Thank you for your reply, but actually I have no idea what you are talking about...

    > I'll suggest you learn the decorator part in python first.


    May I assume that you tried to answer in python? If my post is confusing, I apologize. But I'm talking about C++, not python. Please point out if I'm wrong.

    ---------------------
    Posted by Haochen Xie
    Haochen Xie, Mar 16, 2013
    #3
  4. Haochen Xie

    Öö Tiib Guest

    On Saturday, 16 March 2013 19:45:42 UTC+2, Haochen Xie wrote:
    > ... I'm trying to declare a lambda function returning a lambda function, and,
    > unfortunately, I could not use auto key word since it would be the return
    > type of my function. The lambda function looks like this:
    >
    > typedef std::function<void(string)> Func;
    > vector<pair> set;
    > auto lambda = [&set] (string cmd, Func func) {
    > set.push_back(pair(cmd, func));
    > return lambda;
    > };
    > return lambda;


    You are trying to make infinite recursion that does terminate itself somehow?
    No way. Maybe in some functional language it is fine but in C++ the world
    is finite thing. Different religion ... you see. All unterminated recursions and
    infinite cycles are undefined behavior and belong to world of inefficient
    dreamers and such.
    Öö Tiib, Mar 16, 2013
    #4
  5. Haochen Xie

    Haochen Xie Guest

    On Saturday, March 16, 2013 12:26:34 PM UTC-7, Öö Tiib wrote:

    > You are trying to make infinite recursion that does terminate itself somehow?
    > No way. Maybe in some functional language it is fine but in C++ the world
    > is finite thing. Different religion ... you see. All unterminated recursions and
    > infinite cycles are undefined behavior and belong to world of inefficient
    > dreamers and such.


    Nope. That is not a loop at all. All I want to do is to simulate some code like this without defining a new type (code also available at http://pastebin.com/5NBAiwCZ):

    typedef std::function<void(std::string)> Func;
    class Menu
    {
    public:
    struct Entry
    {
    std::string cmd;
    Func func;
    };
    const class AppendHelper
    ^^^^^^^^^^^^ <- the helper type
    {
    public:
    AppendHelper(Menu &menu) : menu(menu) {}
    const AppendHelper &operator()(std::string cmd, Func func) const
    { menu._command_set.push_back({cmd, func}); return *this; }
    private:
    Menu &menu;
    } append_menu = {*this};

    // For debugging
    void list_commands()
    {
    for(Entry e : _command_set) {
    printf("%s\t: %X\n", e.cmd.c_str(), &e.func);
    }
    }

    private:
    std::vector<Entry> _command_set;
    };

    So that I can have the following code:

    Menu menu;
    menu.append_menu
    ("hehe", [] (std::string input) {printf("hehe");})
    ("haha", [] (std::string input) {printf("haha");});

    I want to try to use lambda function to do this, but that seems to be impossible... Well, anyway, that should be very hard (if possible) in a strong typed language like c++.

    On Saturday, March 16, 2013 2:15:54 PM UTC-7, Andy Champ wrote:
    >
    > Back to your code... your lambda won't get instantiated until the second
    > return statement. Most likely the compiler doesn't see the problem until
    > then.


    Yeah, that's probably why the code compiled for me when I tried yesterday. But, even without the returning line, it doesn't compile now... I should have got something wrong.

    Thank you guys anyway. It's always fun to explore how dirty you can get yourself with c++ ;)
    Haochen Xie, Mar 17, 2013
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Roman Suzi
    Replies:
    13
    Views:
    601
    Bengt Richter
    Jan 7, 2005
  2. Replies:
    11
    Views:
    662
    Christos Georgiou
    May 2, 2006
  3. Mark Piffer
    Replies:
    9
    Views:
    905
    luserXtrog
    May 15, 2009
  4. wiegmale
    Replies:
    0
    Views:
    341
    wiegmale
    Jul 8, 2010
  5. Steve Dogers

    lambda vs non-lambda proc

    Steve Dogers, Mar 30, 2009, in forum: Ruby
    Replies:
    1
    Views:
    173
    Sean O'Halpin
    Mar 30, 2009
Loading...

Share This Page