Simulating nested functions in C++
Local functions are not allowed in C++, but local classes are and function are allowed in local classes. So:
int foo( int foo_var )
{
/*code*/
struct local
{
static int bar( int bar_var )
{
/*code*/
return bar_var;
}
};
return local::bar(foo_var);
}
In C++0x, you would also have the option of creating a functor using lambda syntax. That's a little more complicated in C++03, but still not bad if you don't need to capture variables:
int foo( int foo_var )
{
/*code*/
struct bar_functor
{
int operator()( int bar_var )
{
/*code*/
return bar_var;
}
} bar;
return bar(foo_var);
}
Turn your function into a functor as Herb Sutter suggests in this article