I have a type called MyType that is an int:
typedef int MyType;
It gets treated as an int throughout the code.
I'd like to be able to overload an operator separately for both int and MyType:
class C
{
public:
C& operator>>(int i) {}
C& operator>>(MyType i) {}
};
but I can't:
overload.cpp:7:7: error: 'C& C::operator>>(MyType)' cannot be overloaded
overload.cpp:6:7: error: with 'C& C::operator>>(int)'
Can I have my cake and eat it too?
As per usual, you need a strong typedef. One implementation of that is BOOST_STRONG_TYPEDEF.
As @Cat++ said, BOOST_STRONG_TYPEDEF is a good solution. However for those who don't want to rely on boost library (no mistake if you do) it is easier to do it this way:
#define TYPEDEF(original_type,target_type)struct target_type{\
original_type value;\
explicit target_type(const original_type val):value(val){}\
target_type(){}\
target_type(const target_type& other):value(other.value){}\
target_type& operator=(const original_type& rhs){value = rhs; return *this;}\
target_type& operator=(const target_type& rhs){value = rhs.value; return *this;}\
bool operator==(const target_type& rhs){return value == rhs.value;}\
bool operator<(const target_type& rhs){return value < rhs.value;}\
operator const original_type&() const{return value;}\
operator original_type&(){return value;}\
}
Usage: TYPEDEF(int, MyType);
So What Does this Macro Do?
This macro basically creates a new structure that can easily be treated as the original type in many ways. And to the compiler this is a totally different type.
Take your MyType for instance. When you declare it as typedef int MyType;, at compile time, MyType gets replaced by int. And since you already have a function defined with int as the parameter, it would complain about duplicate function body.
When you do TYPEDEF(int, MyType);, MyType is a structure that can be initialized and assigned a value of int type and would be treated by the compiler as a totally different data-type from int. Yet you will be able to assign it int values because of the operator = function and retrieve its value as an int because of its operator const original_type&() const and operator original_type&() functions.
This roughly what boost does with its BOOST_STRONG_TYPEDEF macro but it seems to be inheriting from other internal classes and I'm not sure what those classes does. But this is a crude solution for those who don't want to rely on boost for whatever reason.
My Personal Recommendation: Use boost whenever possible.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With