(C++) Possible to duplicate functionality of function pointers for templated fcns?
Assume I have a templated family of sort fcns such as
Code:
template <class Bi, class Cmp> insertion_sort(Bi begin, Bi end, Cmp cmp); I.e., in main I would check the command-line args, and say one specifies to do quicksort. I would like to do something along the lines of: Code:
sort = &quicksort; Code:
// lt is an object of type std::less<T> Code:
// lst is a doubly-linked list Code:
switch(sort_type) { Can fcn objects be used to duplicate the functionality of generic fcn pointers (since I can't just create a templated fcn pointer)? |
There may be different ways of looking at this problem but I think that you will need to have a proper class hierarchy so that you iterators are specialised and that you have a sort function that expects a pointer to the the generalised version.
sort (Iter, Cmp) and your iterators Bi and Ran inherit Iter. That way the signature of your functions will match and you should then be able to achieve what you require. |
Do you want it to choose the algorithm automatically based on iterator type? You'll need some ugly compile-time template tricks for that, I'm afraid, since random-access sort functions won't compile with bidirectional iterators. You may need something like this:
Code:
#include <iterator> PS I think I probably misunderstood the question. Have you looked into using a functor? That would probably be the easiest way to get it done. |
All times are GMT -5. The time now is 02:38 PM. |