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. 3 1104
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
"Kai-Uwe Bux" <jk********@gmx.netskrev i en meddelelse
news:g9**********@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.
mlt wrote:
>
"Kai-Uwe Bux" <jk********@gmx.netskrev i en meddelelse
news:g9**********@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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Michael |
last post by:
Ok,
I understand the function pointers in C, but i'm a bit more confused in c++.
Firstly, as i understand it, a static member function can be
"function-pointed" to as a normal c function, but in...
|
by: LinusLee |
last post by:
class CAnalyzer
{
// attribute
private:
public:
// behavior
private:
void NullFunc(NODE *pNode);
void TraverseNode(NODE* pNode, void (*preFunc)(NODE *node), void
(*postFunc)(NODE *node));
|
by: Chris Morley |
last post by:
Hi, I have always done my C++ class callbacks with the age old 'using this
pointer in parameter of the class's static callback function' and
typecasting it to get the specific instance.
However...
|
by: Honestmath |
last post by:
Hello everyone,
I'm using a root finding algorithm in a function that only takes as arguments a
function pointer and two variables that represent guesses at the roots of the
function.
int...
|
by: Oliver Kowalke |
last post by:
Hello,
I've to use a function from c-library (third party) which expects a
function pointer (void(*FunctionPointer)(int)). Instead of using a
global function (with the signature of the function...
|
by: Roshan Naik |
last post by:
typedef int foo ( foo ); // foo is a pointer-to-function type that takes
another foo as argument and returns an int
I need to achieve the above effect somehow. This is not accepted by any...
|
by: Protoman |
last post by:
Which is more efficient for stuff like callbacks, selecting a funct
from a list, and other stuff, function objects or function pointers?
Thanks!!!
|
by: alan.patterson5 |
last post by:
I have a vector of N values and a vector of N functors. I want to apply
each functor to its corresponding value. What would be the 'correct'
STL way to do this.
for_each takes on functor and...
|
by: tryptik |
last post by:
Hello all,
I have a question about iterators. I have a container of functors
that operate on an std::string. The functors go something like this:
class Functor {
std::string...
|
by: chsalvia |
last post by:
In generic programming, static member functions and functors seem to
be very useful. I've noticed that some libraries take the approach of
using templated static member functions to provide...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |