I am working on a data structure and I want it to have comparison function, that can be passed to the constructor, the same way as stl data structures (set, queue, etc.) work. I also want a default function provided in the constructor.
The problem is, that the query to the structure is a function template, that takes custom type as a parameter. But I don't know how to provide the default comparison function if I don't know what type the query will be. Is there a way to do this?
This works if the Query is the same as the NodeType (Query is useless in this example):
template<class NodeType, class Query = NodeType>
inline float eucDistance(const Query * p1, const NodeType * p2) {
...
}
template<class NodeType, typename Comp = decltype(eucDistance<NodeType>)>
class KDTree {
Comp* distance;
public:
KDTree(Comp comaprator = &eucDistance<NodeType>) : distance(comaprator) {
}
template<class Query = NodeType>
NodeType * nearestNeighbor(const Query *query) {
...
float tmp = distance(query, sth);
//I want something like distance<NodeType, Query>(query, sth);
...
}
}
But I would like to do something like this:
class Point; //some type that the tree contains
KDTree<Point> tree;
Point p1;
Point *p = tree.nearestNeighbor(&p1); //this works with the given example
...
vec3 vec = ...; //some different type as a query
p = tree.nearestNeighbor<vec3>(vec); // <-- this is what I would like to do