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

Recommended Posts

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.

Share on other sites

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.

Share on other sites

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

Share on other sites
        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.

Share on other sites
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;
}


Share on other sites

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.

Edited by Ectara

Share on other sites
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.

Share on other sites
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.

Share on other sites
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.

Share on other sites

[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.

Share on other sites
Again, it depends on what semantics you want to support for your class, but if you want to match the built-in behavior of 64-bit ints as closely as possible, then you'll want to handle the conversion of signed and unsigned types differently for an unsigned 64-bit int class. For example, a signed char with the value -1 will convert to 0xFFFFFFFF as a 32-bit unsigned long but 0xFFFFFFFFFFFFFFFF as a 64-bit unsigned long long. If you convert from a signed char to an unsigned long and then convert that to an unsigned long long you're unsigned long long will only be 0xFFFFFFFF.

Share on other sites
Again, it depends on what semantics you want to support for your class, but if you want to match the built-in behavior of 64-bit ints as closely as possible, then you'll want to handle the conversion of signed and unsigned types differently for an unsigned 64-bit int class.

Yeah, I'm aware of such limitations. It provides a two's complement 64bit number, with easy ways to read/write the high and low 32bits, but it doesn't pretend to be a perfect replacement.

[quote name='SiCrane' timestamp='1357409845' post='5017823']
For example, a signed char with the value -1 will convert to 0xFFFFFFFF as a 32-bit unsigned long but 0xFFFFFFFFFFFFFFFF as a 64-bit unsigned long long. If you convert from a signed char to an unsigned long and then convert that to an unsigned long long you're unsigned long long will only be 0xFFFFFFFF.
[/quote]
My code for both signed and unsigned variants show no problems with converting from their respective types. If you convert from a signed char to a signed long, then a signed long long before conversion to an unsigned long long.
http://www.viva64.com/en/a/0065/#ID0ECKHM
A good example can be seen here, that often the promotion occurs before sign extension when going to long or larger types. If one converts the signed value to a signed Int64 before changing its signedness, the behavior works fine.

Share on other sites
Yeah, I'm aware of such limitations. It provides a two's complement 64bit number, with easy ways to read/write the high and low 32bits, but it doesn't pretend to be a perfect replacement.
In that case, using a 64-bit type if available in place of your class becomes a possible source of subtle and annoying errors.
If you convert from a signed char to a signed long, then a signed long long before conversion to an unsigned long long.
That's not what you are doing in your code, as posted. If your fundamental type is unsigned, which it is in the code you've posted, then you'd convert a signed integer type to an unsigned long and then convert that to your 64-bit type, losing information about whether the original type was signed or unsigned.

Share on other sites

[quote name='SiCrane' timestamp='1357417141' post='5017862']
In that case, using a 64-bit type if available in place of your class becomes a possible source of subtle and annoying errors.
[/quote]
That responsibility is on the code that uses the functionality to ensure that it doesn't run into an unsupported feature; _very_ few things I use involve using a 64bit type on a 32bit machine.

[quote name='SiCrane' timestamp='1357417141' post='5017862']
That's not what you are doing in your code, as posted. If your fundamental type is unsigned, which it is in the code you've posted, then you'd convert a signed integer type to an unsigned long and then convert that to your 64-bit type, losing information about whether the original type was signed or unsigned.
[/quote]
In that case, in lieu of doing it explicitly, what would you suggest, that would also allow me to pass in a floating point number, for example, as well?

Share on other sites
That responsibility is on the code that uses the functionality to ensure that it doesn't run into an unsupported feature; _very_ few things I use involve using a 64bit type on a 32bit machine.
The problem here isn't supported versus unsupported. Conversion from other integer types is something that you've decided to support. You just seem to want to support it in a way that works differently than a built-in type. Conversion between types isn't exactly an uncommon operation, and this isn't exactly a small difference in behavior you're proposing.
In that case, in lieu of doing it explicitly, what would you suggest, that would also allow me to pass in a floating point number, for example, as well?
I already said: do conversion between signed and unsigned integers differently. If you cast everything to a single fundamental type then you lose signed/unsigned information which changes how conversion should work. However, you can use type traits to determine if a conversion is being done from a signed or unsigned type. If you want to take the short cut of converting to an int type to do your floating point type conversions I'd recommend converting through a signed type. However, that won't handle many floating point conversions correctly.

Share on other sites
That responsibility is on the code that uses the functionality to ensure that it doesn't run into an unsupported feature; _very_ few things I use involve using a 64bit type on a 32bit machine.
The problem here isn't supported versus unsupported. Conversion from other integer types is something that you've decided to support. You just seem to want to support it in a way that works differently than a built-in type. Conversion between types isn't exactly an uncommon operation, and this isn't exactly a small difference in behavior you're proposing.
In that case, in lieu of doing it explicitly, what would you suggest, that would also allow me to pass in a floating point number, for example, as well?
I already said: do conversion between signed and unsigned integers differently. If you cast everything to a single fundamental type then you lose signed/unsigned information which changes how conversion should work. However, you can use type traits to determine if a conversion is being done from a signed or unsigned type. If you want to take the short cut of converting to an int type to do your floating point type conversions I'd recommend converting through a signed type. However, that won't handle many floating point conversions correctly.

Hm... would making type traits for all built-in C++ types cover typedefs or classes that use conversion operators to return one of the types?

Share on other sites
typedefs don't create new types only aliases for existing types. For classes that use conversion operators you can use the std::is_convertible<> type trait to determine convertibility.

Create an account

Register a new account

• Partner Spotlight

• Forum Statistics

• Total Topics
627651
• Total Posts
2978405

• 10
• 12
• 22
• 13
• 33