4 ms·
In the past I've sometimes used standard C++ local structs to nest functions. E.g.: int f() { struct local { static int nested_func
by anthay 9y ago
In the past I've sometimes used standard C++ local structs to nest functions. E.g.:
int f()
{
struct local {
static int nested_func()
{
return 123;
}
};
return local::nested_func();
}
- WalterBright 9y agoI've done that, too. It's clumsy and only does half the job (doesn't provide access to locals). At some point one just gets tired of the workarounds :-)
- anthay 9y agoIt's probably a bit late for this reply, but... Another clumsy workaround is to use operator(). Again, standard C++ and this one has access to local variables: int g() { int x, y; struct local { int & a_; int & b_; local(int & a, int & b) : a_(a), b_(b) {} int operator()() { return a_ + b_; } } nested(x, y); x = 99; y = 1; return nested(); } assert(g() == 100);