Skip to main content
gamedev.net
🔒 Locked

Default type for Template class

Started by DOrmisher Dec 1, 2008 at 7:42 AM 9 replies 700+ views
Original Post
DOrmisher
DOrmisher
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
swiftcoder
swiftcoder
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]
DOrmisher
DOrmisher
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
staticVoid2
staticVoid2
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:};
swiftcoder
swiftcoder
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]
DOrmisher
DOrmisher
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
SiCrane
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.
Gage64
Gage64
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.
swiftcoder
swiftcoder
Quote:
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.
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.
Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.