S
StephQ
This is from a thread that I posted on another forum some days ago.
I didn't get any response, so I'm proposing it in this ng in hope of
better luck
The standard explanation is that pointer to functions are hard to
inline for the compiler, and so you won't be able to avoid function
call overhead.
This is an important aspect when you are calling a function very
frequently for evaluation reason: think of the problem of performing
numerical integration of function double f(double x).
Another example is the qsort algorithm with different comparison
functions.
The various approaches that I found are:
1) If you are working with 1 function only:
A) Function object approach:
Suppose myfunc is a class with overloaded operator(), then we consider
template<class Fo>
double integrate(Fo f, int n) {
.....
sum += f(i/n);
.....
};
(I know you can use const Fo& f for better performances....)
and call the integration routine with:
integrate(myfunc(),10);
First question: what is myfunc() ? An object of type myfunc? Why?
I would have used:
myfunc f;
integrate(f, 10);
The object has to be created (as an obj of type myfunc is passed to
the function).
If I understand correctly integrate(f, 10) = integrate<myfunc>(f, 10)
as the argument is deduced....
B) Pointer to function as template parameter approach:
Suppose myfunc is a function (take and returns double), then
template<double fp(double)>
double integrate(int n) {
.....
sum += fp(i/n);
.....
};
integrate<myfunc>(10);
I understand the function obj approach is more general: here I just
have the function, previously I had a object (that can embed data and
functions).
Question: In this situation myfunc is not passed to integrate (more
precisely no pointer to this function is passed...). It's like when we
use template parameters for classes. I expect a "substitution" of fp
with the passed myfunc as the code is generated (like for template
classes).
Does this means that we have to distinguish between template arguments
objects that gets passed to the function and template arguments that
are "substituted" word by word like in classes? (see below D)
2) Now to the case where I'm passing more functions (like a function
and the first derivative, they belongs to the same "object").
C) Class as template parameter approach:
Suppose myclass is a class with member functions f1 f2 (as usual take
and return)
template<class fclass>
double integrate(int n) {
.....
sum += fclass::f1(i/n) + fclass::f2(i/n);
.....
};
integrate<myclass>(10);
Here again is it working like situation B?
In B I had a template parameter of type pointer to function, while
here I have a template type parameter.
Maybe the point is that, even if no object here is created I'm able to
use static data inside the class, and define functions f1, f2 that
depends on this data? (while previously I had no class, just the
pointer to function so this possibility was precluded...)
D) A more general apporach:
Suppose myclass is a class with member funcions f1 f2 (as usual double
double)
template<class fclass, double (flcass::*fun1)(double),
double(fclass::*fun2)(double)>
double integrate(int n, const fclass& fc) {
.....
sum += (fc.*fun1)(i/n) + (fc.*fun2)(i/n);
.....
};
myclass mc;
integrate<myclass, &myclass::f1, &myclass::f2>(10, mc);
I understand this approach is more general: not only I choose the
class, but also I can choose the functions of the class that I'm using
in integrate.
Also I create the obj class, so I can have different objects of the
same class with different data member values (while previously only
static data could be used).
I understand his use of pointers to member functions.
So do we need to consider:
fc.*fun1
because I'm considering the particular obj mc of type myclass (and so,
even if the function of every obj of type myclass are the same, they
may depend on different data inside this obj) ?
Do you have any general comment on these approaches?
Thank you in advance for any help!
Best Regards
StephQ
I didn't get any response, so I'm proposing it in this ng in hope of
better luck
The standard explanation is that pointer to functions are hard to
inline for the compiler, and so you won't be able to avoid function
call overhead.
This is an important aspect when you are calling a function very
frequently for evaluation reason: think of the problem of performing
numerical integration of function double f(double x).
Another example is the qsort algorithm with different comparison
functions.
The various approaches that I found are:
1) If you are working with 1 function only:
A) Function object approach:
Suppose myfunc is a class with overloaded operator(), then we consider
template<class Fo>
double integrate(Fo f, int n) {
.....
sum += f(i/n);
.....
};
(I know you can use const Fo& f for better performances....)
and call the integration routine with:
integrate(myfunc(),10);
First question: what is myfunc() ? An object of type myfunc? Why?
I would have used:
myfunc f;
integrate(f, 10);
The object has to be created (as an obj of type myfunc is passed to
the function).
If I understand correctly integrate(f, 10) = integrate<myfunc>(f, 10)
as the argument is deduced....
B) Pointer to function as template parameter approach:
Suppose myfunc is a function (take and returns double), then
template<double fp(double)>
double integrate(int n) {
.....
sum += fp(i/n);
.....
};
integrate<myfunc>(10);
I understand the function obj approach is more general: here I just
have the function, previously I had a object (that can embed data and
functions).
Question: In this situation myfunc is not passed to integrate (more
precisely no pointer to this function is passed...). It's like when we
use template parameters for classes. I expect a "substitution" of fp
with the passed myfunc as the code is generated (like for template
classes).
Does this means that we have to distinguish between template arguments
objects that gets passed to the function and template arguments that
are "substituted" word by word like in classes? (see below D)
2) Now to the case where I'm passing more functions (like a function
and the first derivative, they belongs to the same "object").
C) Class as template parameter approach:
Suppose myclass is a class with member functions f1 f2 (as usual take
and return)
template<class fclass>
double integrate(int n) {
.....
sum += fclass::f1(i/n) + fclass::f2(i/n);
.....
};
integrate<myclass>(10);
Here again is it working like situation B?
In B I had a template parameter of type pointer to function, while
here I have a template type parameter.
Maybe the point is that, even if no object here is created I'm able to
use static data inside the class, and define functions f1, f2 that
depends on this data? (while previously I had no class, just the
pointer to function so this possibility was precluded...)
D) A more general apporach:
Suppose myclass is a class with member funcions f1 f2 (as usual double
double)
template<class fclass, double (flcass::*fun1)(double),
double(fclass::*fun2)(double)>
double integrate(int n, const fclass& fc) {
.....
sum += (fc.*fun1)(i/n) + (fc.*fun2)(i/n);
.....
};
myclass mc;
integrate<myclass, &myclass::f1, &myclass::f2>(10, mc);
I understand this approach is more general: not only I choose the
class, but also I can choose the functions of the class that I'm using
in integrate.
Also I create the obj class, so I can have different objects of the
same class with different data member values (while previously only
static data could be used).
I understand his use of pointers to member functions.
So do we need to consider:
fc.*fun1
because I'm considering the particular obj mc of type myclass (and so,
even if the function of every obj of type myclass are the same, they
may depend on different data inside this obj) ?
Do you have any general comment on these approaches?
Thank you in advance for any help!
Best Regards
StephQ