Operators for non-primitive boxed types

Viewed 390

I have the following class definition:

template <typename T>
class MyBox {
public:
    MyBox(T value) { _value = value; }
    operator T() const { return _value;  }
private:
    T _value;
};

typedef MyBox<int> MyInt;
typedef MyBox<std::string> MyString;

When I try to use operators on my typedefs like this

bool first = MyInt(1) == MyInt(1);    // works
bool second = std::string(MyString("a")) == std::string(MyString("a"));    //works
bool third = MyString("a") == MyString("a");   // does not compile

the compiler complains about the third comparison

no operator "==" matches these operands. operand types are: MyString == MyString

and this happens with any other non-primitve boxing (e.g. MyBox<float> works but MyBox<std::map<int,int> > not. Why is that so?

This is especially unclear to me because for the first and second comparison the operator T() is used - why can't that be done automatically for MyString as well?

UPDATE: Is there a simple solution to this other than providing the specific operators for each non-primitive template? And what to do with MyString("a") == std::string("a")?

1 Answers
Related