At first, can somone explain me what (*const) means or where I can read about this?
In the author's example the distance function is:
float euclidean(const std::vector<float>&, const std::vector<float>&);
The argument passed to the CoverTree
constructor is the address of that function i.e. &euclidean
, which is a function pointer of type
float (*)(const std::vector<float>&, const std::vector<float>&)
The template parameter for CoverTree
is simply a const
-qualified version of that type. Due to the peculiar "inside out" declarator syntax for functions in C and C++, a const pointer to that function type is declared as:
float (* const)(const std::vector<float>&, const std::vector<float>&)
This is analogous to a const
pointer to int
, which is declared as:
int* const
In your case, you got the type right, it's:
boost::function<float (const vector<Frame>::const_iterator&, const vector<Frame>::const_iterator&)
Let's use a typedef to refer to that:
typedef boost::function<float (const vector<Frame>::const_iterator&, const vector<Frame>::const_iterator&) distance_func;
but the problem is that you are not passing an argument of that type, you're passing a pointer to that type:
CoverTree<float, vector<Frame>::const_iterator, distance_func> tree(&j_dist);
^^^^^^^
You can see this won't work by doing:
distance_func f = &j_dist; // ERROR! cannot convert distance_func* to distance_func
The answer should be to simply to pass an argument of the correct type:
CoverTree<float, vector<Frame>::const_iterator, distance_func> tree(j_dist);
^^^^^^