开发者

User Defined Class as a Template Parameter

开发者 https://www.devze.com 2022-12-30 18:41 出处:网络
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 th

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 开发者_如何学Ctype 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?


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号