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.
🔒 Locked
Default type for Template class
Advertisement
Original Post
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?
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:Why? 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.
Original post by staticVoid2
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.
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.
Quote:Hmm, good point. I seem to recall using regular functions as the comparator for std::set in the past, but I see this doesn't work either.
Original post by SiCrane 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.
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement