Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Type of lambda function returning a lambda function...

Reply
Thread Tools

Type of lambda function returning a lambda function...

 
 
Haochen Xie
Guest
Posts: n/a
 
      03-16-2013
.... 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
 
Reply With Quote
 
 
 
 
88888 Dihedral
Guest
Posts: n/a
 
      03-16-2013
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.
 
Reply With Quote
 
 
 
 
Haochen Xie
Guest
Posts: n/a
 
      03-16-2013
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
 
Reply With Quote
 
Tiib
Guest
Posts: n/a
 
      03-16-2013
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.
 
Reply With Quote
 
Haochen Xie
Guest
Posts: n/a
 
      03-17-2013
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++
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
returning pointer of template type function header wiegmale C++ 0 07-08-2010 09:10 PM
function returning function pointer (recursive type) Mark Piffer C Programming 9 05-15-2009 07:54 AM
lambda vs non-lambda proc Steve Dogers Ruby 1 03-30-2009 10:11 PM
write a function such that when ever i call this function in some other function .it should give me tha data type and value of calling function parameter komal C++ 6 01-25-2005 11:13 AM
Re: Lambda as declarative idiom (was RE: what is lambda used for inreal code?) Roman Suzi Python 13 01-07-2005 09:33 PM



Advertisments