template< typename T, typename F >
class CMultiset
However, I want the class to sort the class in ascending order by default. Is there anyway I can make the F typename automatically a type of my own class that does the ascending sort?? So when an object of CMultiset is being created you don't have to specify the type of F if you don't want.
Cheers guys.
Default type for Template class
I'm creating a multiset class that is sorted with a function object. T is the type and F the function object:
Something like:
Should do the trick, but keep in mind that the std::library already has a perfectly good multi_set, so why reinvent the wheel?
template <typename T> bool sort_func(const T &a, const T &b) {return a < b;}template <typename T, typename F = sort_func<T> >class Multiset{};
Should do the trick, but keep in mind that the std::library already has a perfectly good multi_set, so why reinvent the wheel?
Ahh that simple? I was expected something convoluted...
And I would never re-invent the wheel unless my tutor was forcing me too :)
Cheers dude.
And I would never re-invent the wheel unless my tutor was forcing me too :)
Cheers dude.
Quote:Original post by swiftcoder
Something like:
*** Source Snippet Removed ***
Should do the trick, but keep in mind that the std::library already has a perfectly good multi_set, so why reinvent the wheel?
do you not mean:
template <typename T> bool sort(T a, T b) { return a < b; }template <typename T, bool (*F)(T a, T b) = sort<T>>class CMultiSet{public:};
Quote:Original post by staticVoid2Why? That would restrict the comparator to be a pointer to a 2-argument function. By allowing any type, we can accept functors as well as function pointers, and we can accept 3+ argument functions with default arguments.
do you not mean:template <typename T, bool (*F)(T a, T b) = sort<T>>
Also beware that you need to place a space between the two closing angle brackets - otherwise your compiler may interpret it is a right-shift, due to an unfortunate loophole in the C++ grammar.
Cheers guys, just one more thing. Is there anyway of checking if an operator has been defined / overloaded for a type so you can jump straight out of a function if, say a user defined type is passed to the template class but it has no < or == operators defined??
Quote:Original post by swiftcoder
Why?
He's probably asking because a function pointer isn't a type, so can't be used with a typename template argument, thus making your example fail to compile.
Quote:Original post by DOrmisher
Is there anyway of checking if an operator has been defined / overloaded for a type so you can jump straight out of a function if, say a user defined type is passed to the template class but it has no < or == operators defined??
If the type doesn't support the operator, you will get a compile error, so no check is needed.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement