I have this piece of code:
#include <iostream>
using namespace std;
template <typename T1, typename T2>
class Number {
T1 value;
public:
Number(const T1& val): value(val){};
operator T2() { return (T2)(value / 2); }
T1 operator+(const T1& val) { return value + val; }
};
int main() {
Number<int, float>n1(10);
Number<float, int>n2(3.0);
cout << n1 + n2 << ", " << n2 + n1 << ", " << n1 - n2 << endl;
}
In spite having no subtraction operator overloaded this code still works and outputs:
11, 8, 4.
Shouldn't it throw an error at compile time ?
Any idea will be appreciated.