Type of lambda function returning a lambda function...

H

Haochen Xie

.... 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.
 
8

88888 Dihedral

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.

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.
 
H

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.

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.
 
Ö

Öö Tiib

... 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.
 
H

Haochen Xie

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++.

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++ ;)
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,520
Members
44,996
Latest member
rainocode

Latest Threads

Top