I need to pass a distance-function to a template. Therefore I use boost::function and boost::bind. But I do not understand what I have to pass for class Distance:
template<class DataType, class Point, class Distance>
class CoverTree
{
Distance distance;
...
public:
CoverTree(const Distance& distance) : max_level(default_max_level), min_level(default_max_level), distance(distance) {}
...
}
the example by the author of the template looks like this:
float euclidian(const std::vector<float>& p1, const std::vector<float>& p2)
{
...
}
int main(int argc, char** argv)
{
CoverTree<float, std::vector<float>, float (*const)(const std::vector<float>&, const std::vector<float>&)> tree(&euclidian);
...
}
Now this is my main:
int main(int argc, char** argv)
{
AllData myData;
boost::function<float (const vector<Frame>::const_iterator&, const vector<Frame>::const_iterator&)> j_dist;
j_dist = boost::bind(&AllData::jaccard_distance, myData, _1, _2);
myData.AddData("C:\\...");
cout<<j_dist(myData.DATAx.begin()+20, myData.DATAx.begin()+40)<<endl; //works fine
CoverTree<float, vector<Frame>::const_iterator, ???> tree(&j_dist);
...
}
At first, can somone explain me what (*const) means or where I can read about this?
And second:
I think I wrote everything you need, to tell what to write for ??? but I don’t get it.
I already tried:
boost::function<float (const vector<Frame>::const_iterator&, const vector<Frame>::const_iterator&)
and
float (*const) (const vector<Frame>::const_iterator&, const vector<Frame>::const_iterator&)
but this was nice try and error 🙂
In the author’s example the distance function is:
The argument passed to the
CoverTreeconstructor is the address of that function i.e.&euclidean, which is a function pointer of typeThe template parameter for
CoverTreeis simply aconst-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:This is analogous to a
constpointer toint, which is declared as:In your case, you got the type right, it’s:
Let’s use a typedef to refer to that:
but the problem is that you are not passing an argument of that type, you’re passing a pointer to that type:
You can see this won’t work by doing:
The answer should be to simply to pass an argument of the correct type: