mlt wrote:
> I have a function that computes the distance between some objects. The
> distance could be euclidian, manhaten etc. I would therefore like to
> implement the distance metric as a functor as seen below:
>
> template<typename dist>
> function compute_distance(double a, double b)
> {
> dist dist_measure;
> double distance = dist_measure(a,b);
> return distance;
> }
>
>
> But can 'dist' just be a separate function or should be a class? Since its
> use as a template parameter I assume it must be a type/class and cannot be
> a simple function.
The way you set it up, the template parameter has to be a class.
a) I do not exactly see what the above function buys you. You would have to
invoke it like so:
compute_distance< dist >( a, b )
which is just marginally better than
dist()( a, b )
and worse than
dist( a, b )
b) To answer your technical question: you can use a function as a template
parameter:
template < double (dist) ( double, double ) >
double f ( double a, double b ) {
return ( dist(a,b) );
}
double p1 ( double a, double b ) {
return ( a );
}
#include <iostream>
int main ( void ) {
std::cout << f<p1>( 0.1, 0.2 ) << '\n';
}
However, that does not buy you anything either. What is the advantage of the
more convoluted:
f<p1>( a, b )
over:
p1( a, b )
Best
KaiUwe Bux
