Creating A User-Defined Class That Mimics Built-in Types' Behavior

Started by
15 comments, last by SiCrane 11 years, 3 months ago

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.

Advertisement

Did you try typecasting to Int64 or int, and see how the product value changes?

It will help if you can show your class implementation.

You might not want overloaded constructors, but they're the only way you can use the explicit keyword.

Stephen M. Webb
Professional Free Software Developer


        struct Int64{
                typedef unsigned long fundamentalType;
                
                unsigned int i32_[2];
                
                Int64 & operator =(const Int64 & other);
                Int64 & operator =(fundamentalType other);
                
                operator fundamentalType(void);
        };

        Int64 operator *(const Int64 & x, const Int64 & y);
        Int64 operator *(const Int64 & x, typename Int64::fundamentalType y);
        Int64 operator *(typename Int64::fundamentalType y, const Int64 & x);

The type names aren't their actual names, just simplified for easier understanding.

[quote name='NewDisplayName' timestamp='1357380875' post='5017695']
Did you try typecasting to Int64 or int, and see how the product value changes?
[/quote]
I just tried it out of curiosity, and casting to Int64 failed as expected, because how can I typecast an integral result to Int64 if Int64 has no constructor that accepts it? Casting to int didn't have any effect, since the problem occurs before the cast takes place.

[quote name='Bregma' timestamp='1357392381' post='5017732']
You might not want overloaded constructors, but they're the only way you can use the explicit keyword.
[/quote]
Yes, explicit is nice, but I can't see how that would alleviate the current issue.

You could add in either explicit int overloads or some template operator overloads. Ex:

template <typename T>
Int64 operator *(const Int64 & x, T y) {
  Int64 temp;
  temp = y;
  return (x * temp);
}

template <typename T>
Int64 operator *(T x, const Int64 & y) {
  return y * x;
}

You could add in either explicit int overloads or some template operator overloads. Ex:


template <typename T>
Int64 operator *(const Int64 & x, T y) {
  Int64 temp;
  temp = y;
  return (x * temp);
}

template <typename T>
Int64 operator *(T x, const Int64 & y) {
  return y * x;
}

The thing is, I did (the last two overloads of operator * in my posted code), however, the compiler declares it ambiguous whether it should use these overloads, or use the conversion operator to convert the Int64 to the fundamental type, then do built-in math with it.

unsigned long != int. An int overload is different from an unsigned long overload. One will match int exactly and the other will match unsigned long exactly.
unsigned long != int. An int overload is different from an unsigned long overload. One will match int exactly and the other will match unsigned long exactly.

Ah! That is where the problem lies. When you consider the conversions between int and long, the amount of implicit conversions make it ambiguous (though G++ admits that using the overloaded function was the most likely intention).

The code now looks like (simplified):


template <typename T> Int64 operator *(const Int64 & x, T y);
template <typename T> Int64 operator *(T x, const Int64 & y);

and inside the template function, it static_cast's the templated type to an instance of the fundamental type, and performs the operation as expected. Is using the cast the right way to do it? I ultimately want it to be of the fundamental type before I do any work with it, because this is the built-in type with essentially the closest compatibility to the Int64 type.


It's hard to say if that's the right thing or not. It depends on what semantics you intend to support. My gut instinct is that you'll want to handle conversions to Int64 differently for signed and unsigned types.

Also, since you mention that you're using g++, C++11 added long long, which is guaranteed to be at least 64-bits and gcc supports long long in C++ code as an extension even when not compiling in C++11 standard mode.

[quote name='SiCrane' timestamp='1357406826' post='5017801']
It's hard to say if that's the right thing or not. It depends on what semantics you intend to support. My gut instinct is that you'll want to handle conversions to Int64 differently for signed and unsigned types.
[/quote]
I would think it is the behavior that I'm seeking. In actuality, the Int64 class is templated, with a tag parameter that affects whether or not it is signed; since most of the code is the same for signed and unsigned, it made sense to have one class, with specializations where they differ. The definition is in a traits class, which is specialized for the signedness, so I expect and encourage fundamentalType to change, both to better suit the purpose for a given platform, and to change signedness.

[quote name='SiCrane' timestamp='1357406826' post='5017801']
Also, since you mention that you're using g++, C++11 added long long, which is guaranteed to be at least 64-bits and gcc supports long long in C++ code as an extension even when not compiling in C++11 standard mode.
[/quote]
Yeah, all of my code will check for the availability of a built-in 64bit type before ever resorting to using this as a fallback. This is mostly an exercise in learning templates; I hope to not have to use it.

This topic is closed to new replies.

Advertisement