Default type for Template class

Started by
9 comments, last by swiftcoder 15 years, 4 months ago
I'm creating a multiset class that is sorted with a function object. T is the type and F the function object:
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.
She got a hat and all that hat says is asshole
Advertisement
Something like:
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?

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

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.
She got a hat and all that hat says is asshole
Or:
template< typename T, typename F = std::less<T> >class CMultiset
yup:

template <typename A, typename B=some_default> class Baz { ... }
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 staticVoid2
do you not mean:
template <typename T, bool (*F)(T a, T b) = sort<T>>
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.

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.

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

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??
She got a hat and all that hat says is asshole
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