In C the following code works consider I always use gcc.
int foo( int foo_var )
{
/*code*/
int bar( int bar_var )
{
/*code*/
return bar_var;
}
return bar(foo_var);
}
How can achieve the same functionality of nested functions in C++ on gcc compiler? Don't mind if this seems like a beginner question. I am new to this site.
Turn your function into a functor as Herb Sutter suggests in this article
AFAIK, nested functions are not allowed in C++.
The construct that comes closest to nested functions is the C++11 lambda.
Lamdas allow to use variables from the outer scope (the [&] specifies to automatically capture all variables from the outer scope by reference). A lambda, that does not use any variables from the outer scope (use []) can be converted to a function pointer of the same type and can thus be passed to functions accepting a function pointer.
In C++ you may achieve the same effect by other possible means. There are no direct nested function implementations. Two helpful links:
http://www.respower.com/~earlye/programming/19990916.001.htm
http://www.devx.com/tips/Tip/40841
use local functor
You could try using boost::phoenix (v2 is a subpackage of spirit, v3 is in svn/trunk as it's own package and should be in 1.47)