User Defined Class as a Template Parameter

2019-07-19 00:46发布

I' m re-implementing std::map. I need to make sure that any data type (basic or user defined) key will work with it. I declared the Map class as a template which has two parameters for the key and the value. My question is if I need to use a string as the key type, how can I overload the < and > operators for string type keys only?? In template specialization we have to specialize the whole class with the type we need as I understand it.
Is there any way I can do this in a better way?? What if I add a separate Key class and use it as the template type for Key?

2条回答
对你真心纯属浪费
2楼-- · 2019-07-19 01:26

You could also use type traits. It will give you a framework to solve also the possible future differences between types.

查看更多
女痞
3楼-- · 2019-07-19 01:28

You should factor out the comparison as a type, like the normal std::map does. That is, have a utility class less_compare:

template <typename T>
struct less_compare
{
    bool operator()(const T& pLhs, const T& pRhs) const
    {
        return pLhs < pRhs;
    }
};

And then:

template <typename Key, typename Value, typename Compare = less_compare<Key> >
class map
{
    // ...

private:
    Compare mCompare;
};

And to compare two values, do: if (mCompare(someThing, someOtherThing)), which will be true with someThing is "less than" someOtherThing. Note this factoring also allows user-defined comparisons (which is why "less than" is quoted). This is known as policy-based design.

And now you can specialize just the less_compare class for C-strings. (And also provide greater_compare and kin.)


Do keep in mind, unless this is for learning, you should not be implementing your own map. Also note that std::string has operator< overloaded already.

查看更多
登录 后发表回答