Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Function functors?

Reply
Thread Tools

Function functors?

 
 
mlt
Guest
Posts: n/a
 
      08-28-2008
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.


 
Reply With Quote
 
 
 
 
Kai-Uwe Bux
Guest
Posts: n/a
 
      08-28-2008
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

Kai-Uwe Bux
 
Reply With Quote
 
 
 
 
mlt
Guest
Posts: n/a
 
      08-28-2008

"Kai-Uwe Bux" <(E-Mail Removed)> skrev i en meddelelse
news:g97352$fhj$(E-Mail Removed)...
> 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 )



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.


 
Reply With Quote
 
Kai-Uwe Bux
Guest
Posts: n/a
 
      08-28-2008
mlt wrote:

>
> "Kai-Uwe Bux" <(E-Mail Removed)> skrev i en meddelelse
> news:g97352$fhj$(E-Mail Removed)...
>> 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 )

>
>
> 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
 
Reply With Quote
 
 
 
Reply

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 On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Function versus pointer to function, in context of std::function,huh? Alf P. Steinbach C++ 10 07-27-2011 05:51 AM
Function pointer to void function and int function Giannis Papadopoulos C Programming 5 09-05-2005 09:06 PM
How override ALL function calls? (Is there a "function call function"?) seberino@spawar.navy.mil Python 2 08-01-2005 12:38 PM
write a function such that when ever i call this function in some other function .it should give me tha data type and value of calling function parameter komal C++ 6 01-25-2005 11:13 AM
Passing a C++ object's member function to a C function expecing a function pointer! James Vanns C++ 7 01-21-2004 02:39 AM



Advertisments