![]() |
Type of lambda function returning a lambda function...
.... 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 |
Re: Type of lambda function returning a lambda function...
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. |
Re: Type of lambda function returning a lambda function...
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 |
Re: Type of lambda function returning a lambda function...
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. |
Re: Type of lambda function returning a lambda function...
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++ ;) |
| All times are GMT. The time now is 04:04 PM. |
Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.