I have a class that I'm writing that implements 64bit arithmetic through an interface like that of a built-in type. My problem occurs with implicit conversions and conversion operators that lead to ambiguities.
As an example of what is happening, I have a fundamental integral type; this type of integer is a built-in type to and from which the 64bit integer will be converted. I have an overload for the = operator in the class that accepts this integer, to allow one to assign from the fundamental integer to my 64bit integer class. However, since I can't extend the capabilities of a built-in type, I added a conversion operator to the built-in type, to allow one to assign from the 64bit integer type to the fundamental integer type. All mathematical operators have overloads where either the left- or the right-hand side is the fundamental integer type, to facilitate using them interchangeably, like you can perform math between and int and a long without worrying too much about the details.
The problem presents itself when I do something like this:
Int64 x = 5; int i; i = x * 10;
The compiler cannot resolve the ambiguity between whether it should multiply x by 10, returning an Int64 (which I'd prefer, even if it'd result in the returned Int64 being truncated to a smaller type to be assigned to the int) or truncate x to a fundamental integer type before multiplying it by 10, and assign it to i.
Ideally, I'd like it to call the overloaded function in the Int64 class, which would in essence be promoting the constant without making a temporary, and return an Int64, much like if I multiplied a double by a float, I'd have a double result.
Additionally, I'd like any solution to avoid using overloaded constructors, as this needs to a POD type that can be used in a union.
Does anyone have any idea how I can achieve the behavior that I desire? I can accept that I may not be doing things the best way.