David Harmon wrote:
Actually, one reason to use function objects is because can be inlined
in some situations where normal functions can't.
Could you show a specific example please?
template <typename F>
void callfun(F *f)
{
(*f)();
}
Now, if you pass a function to this, the function to be called is (*f)
and only by dereferencing the pointer passed can the generated code call
the function.
But, if you pass a functional object the function to be called is
sometype::operator() regardless of the pointer value, and the call can
probably be inlined.
...
This is not a fair comparison. The above version of the code is run-time
parametrized and the call cannot be resolved at compile time (which
prevents inlining). The functional object version is compile-time
parametrized, which makes inlining possible.
Run-time parametrization can be implemented with both ordinary functions
and functional objects (through virtual functions). In both cases
inlining is impossible.
Compile-time parametrization can also be implemented with both ordinary
functions and functional objects. In both cases inlining is possible.
Both run-time and compile-time parametrization has its own uses and its
own advantages/disadvantages.
In order to make fair comparison in our case, you have to make "ordinary
function" version compile-time parametrized. For example, like this
template <char (*F)(int)> char callfun(int i)
{
return F(i);
}
Function call can be inlined in this case.
--
Best regards,
Andrey Tarasevich