Function functors?

M

mlt

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.
 
K

Kai-Uwe Bux

mlt said:
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

Kai-Uwe Bux
 
M

mlt

Kai-Uwe Bux said:
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 )


The point is that a lot more things has to be done in compute_distance and
calculating distance between a and b should be used in later computations in
compute_distance. Based on this and the fact that it should be possible to
change the metric for dist(a,b) I cannot think of a better way than to use a
functor.
 
K

Kai-Uwe Bux

mlt said:
The point is that a lot more things has to be done in compute_distance and
calculating distance between a and b should be used in later computations
in compute_distance. Based on this and the fact that it should be possible
to change the metric for dist(a,b) I cannot think of a better way than to
use a functor.

In that case, you might consider the following signature:

template < typename Func >
some_type do_stuff ( double a, double b, Func dist ) {
...
... dist( a, b ) ...;
...
}

This is most flexible: when you write

do_stuff( a, b, euclidean_dist );

the parameter euclidean_dist could be a function or a functor object. Its
type will be deduced.



Best

Kai-Uwe Bux
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top