Jump to content
  • Advertisement
Sign in to follow this  
DOrmisher

Default type for Template class

This topic is 3607 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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:

};


Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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??

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!