- **C++**
(*http://www.velocityreviews.com/forums/f39-c.html*)

- - **Function functors?**
(*http://www.velocityreviews.com/forums/t632809-function-functors.html*)

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

Re: Function functors?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 |

Re: Function functors?"Kai-Uwe Bux" <jkherciueh@gmx.net> skrev i en meddelelse news:g97352$fhj$1@aioe.org... > 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. |

Re: Function functors?mlt wrote:
> > "Kai-Uwe Bux" <jkherciueh@gmx.net> skrev i en meddelelse > news:g97352$fhj$1@aioe.org... >> 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 |

All times are GMT. The time now is 05:07 PM. |

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.

SEO by vBSEO ©2010, Crawlability, Inc.