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

Started by
15 comments, last by SiCrane 11 years, 3 months ago
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.
Advertisement
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.

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.

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

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

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.

This topic is closed to new replies.

Advertisement