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