Suppose I have the following code that I wish to refactor:
int toFuture()
{
precalc();
int calc = 5 * foobar_x() + 3;
postcalc();
return calc;
}
int toP
There is a C/C++
way to do this, and a C++11
way. Neither way involves lambdas or templates.
The C/C++
way:
double MyFunc (int x, float y) { return x + y ; }
int main()
{
double (*pf) (int, float) ;
pf = MyFunc ;
pf (101, 202.0) ;
}
The C++11
way:
#include
double MyFunc (int x, float y) { return x + y ; }
int main()
{
std::function f ;
f = MyFunc ;
f (51, 52.0) ;
}
In either case, you just pass pf
or f
to your refactored function as a parameter. Using lambdas or templates is overkill here.