In class Number
you define a conversion operator to int
and your constructor allows converting an int
to a Number
. Therefore, when comparing a Number n
and an int x
for equality, ambiguity arises: should the compiler invoke the built-in operator ==
for int
s and convert n
to an int
, or should it rather pick your operator and convert x
to a Number
? Both conversions are equally good, and it can't choose one.
So yes you have to define three versions, or add a template operator which can perfectly match the type of all arguments and forward to your operator explicitly, like this one (but you most likely want to guard it with some enable_if
to limit its applicability only to the appropriate T
and U
):
template<typename T, typename U> // beware: this will match anything. to be constrained
bool operator == (T n, U const& u)
{
return (Number(n) == Number(u));
}