Jump to content
  • Advertisement
Sign in to follow this  
BattleMetalChris

Template class, with a templated object declared inside using a default parameter

This topic is 2531 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 templated container class which wraps a std::map.

I want to be able to specify the key and value types (obviously) but I'd also like to take advantage of the fact that a std::map can use as a key an object with no '<' or '==' implemented by specifying either a function pointer to a compare method or a comparator class for comparing the keys.

I don't always want to specify these though (for example if I'm using int as a key, there's no point specifying how to compare them), but is there any way to pass the 'default-ness' of the template parameter through to the wrapped map's declaration?

For example:


template <typename V, typename K, typename C> // V is value type, K is keytype, C is comparator class
class Container
{
std::map<K, V, C>
};


is nice and simple, but I want to be able to leave out parameter C when I declare a Container it if it's not needed (like, as I said, if the key type is int), but the problem is that the map will still expect the third parameter.

Essentially I kind of want


template <typename V typename K, typename C>
class Container
{
std::map<K, V, C>

};


but at the same time


template <typename V typename K>
class Container
{
std::map<K, V>

};


Is there any default value I can give for C such that the map is declared without the comparator class if I don't specify it when I declare a container?

Share this post


Link to post
Share on other sites
Advertisement
Just set C = std::less<K>. This is what std::map does.

template <typename V, typename K, typename C = std::less<V> >
class Container
{
std::map<K, V, C> data;
};

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!