Suppose we have next code:
struct int64
{
long long value;
int64() : value(0) {}
int64(signed char i8) : value(i8) {}
int64(unsigned char u8) : value(u8) {}
int64(short i16) : value(i16) {}
int64(unsigned short u16) : value(u16) {}
int64(int i32) : value(i32) {}
int64(unsigned u32) : value(u32) {}
int64(long long i64) : value(i64) {}
int64(unsigned long long u64) : value(u64) {}
int64(const int64& i64) : value(i64.value) {}
int64& operator+=(const int64& rhs) { return value += rhs.value, *this; }
int64& operator-=(const int64& rhs) { return value -= rhs.value, *this; }
friend int64 operator+(const int64& lhs, const int64& rhs) { return int64(lhs) += rhs; }
friend int64 operator-(const int64& lhs, const int64& rhs) { return int64(lhs) -= rhs; }
operator char() const { return (char)value; }
operator short() const { return (short)value; }
operator int() const { return (int)value; }
operator long long() const { return value; }
};
when compiling the this code:
int64 q = 500;
int64 m = q + 1024;
an error occurred because there is 4 similar conversion available to 1024 and one for q to integer type, to solve this problem, i removed the operator XX from the int64 and added the following code:
template <typename n>
operator n() const { return (n)value; }
now i can execute the following code:
int64 q = 500;
int64 m = q + 1024;
short r = q;
the template definition works with Visual C++ and GCC compilers but Intel C++ compiler.
how can i write this conversion operators that work for those compilers?
You should write
operator+definitions for all the types you are supporting:You’re adding an
intto anint64and assigning the result to anint64but it doesn’t have a copy constructor so it’s converting it to some integral type and trying to do some weirdness with all those conversion operators and constructors.