Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old August 28th, 2008, 09:35 PM
mlt
Guest
 
Posts: n/a
Default Function functors?

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.


  #2  
Old August 28th, 2008, 09:55 PM
Kai-Uwe Bux
Guest
 
Posts: n/a
Default Re: Function functors?

mlt wrote:
Quote:
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
  #3  
Old August 28th, 2008, 10:15 PM
mlt
Guest
 
Posts: n/a
Default Re: Function functors?


"Kai-Uwe Bux" <jkherciueh@gmx.netskrev i en meddelelse
news:g97352$fhj$1@aioe.org...
Quote:
mlt wrote:
>
Quote:
>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 )

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.


  #4  
Old August 28th, 2008, 10:45 PM
Kai-Uwe Bux
Guest
 
Posts: n/a
Default Re: Function functors?

mlt wrote:
Quote:
>
"Kai-Uwe Bux" <jkherciueh@gmx.netskrev i en meddelelse
news:g97352$fhj$1@aioe.org...
Quote:
>mlt wrote:
>>
Quote:
>>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 )
>
>
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
 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles