Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


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


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
16 replies to this topic

#1 Ectara   Crossbones+   -  Reputation: 3054

Like
0Likes
Like

Posted 05 January 2013 - 03:18 AM

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.



Sponsor:

#2 NewDisplayName   Members   -  Reputation: 359

Like
0Likes
Like

Posted 05 January 2013 - 04:14 AM

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.



#3 Bregma   Crossbones+   -  Reputation: 5385

Like
0Likes
Like

Posted 05 January 2013 - 07:26 AM

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

#4 Ectara   Crossbones+   -  Reputation: 3054

Like
0Likes
Like

Posted 05 January 2013 - 09:48 AM

        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.

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


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.

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


Yes, explicit is nice, but I can't see how that would alleviate the current issue.



#5 SiCrane   Moderators   -  Reputation: 9662

Like
0Likes
Like

Posted 05 January 2013 - 09:58 AM

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;
}


#6 Ectara   Crossbones+   -  Reputation: 3054

Like
0Likes
Like

Posted 05 January 2013 - 10:03 AM

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.


Edited by Ectara, 05 January 2013 - 10:04 AM.


#7 SiCrane   Moderators   -  Reputation: 9662

Like
1Likes
Like

Posted 05 January 2013 - 10:10 AM

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.

#8 Ectara   Crossbones+   -  Reputation: 3054

Like
0Likes
Like

Posted 05 January 2013 - 10:35 AM

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.


 



#9 SiCrane   Moderators   -  Reputation: 9662

Like
0Likes
Like

Posted 05 January 2013 - 11:27 AM

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.

#10 Ectara   Crossbones+   -  Reputation: 3054

Like
0Likes
Like

Posted 05 January 2013 - 11:56 AM

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.


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.

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.


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.



#11 SiCrane   Moderators   -  Reputation: 9662

Like
0Likes
Like

Posted 05 January 2013 - 12:17 PM

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.

#12 Ectara   Crossbones+   -  Reputation: 3054

Like
0Likes
Like

Posted 05 January 2013 - 02:04 PM

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.


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.


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.



#13 SiCrane   Moderators   -  Reputation: 9662

Like
0Likes
Like

Posted 05 January 2013 - 02:19 PM

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.

#14 Ectara   Crossbones+   -  Reputation: 3054

Like
0Likes
Like

Posted 05 January 2013 - 05:30 PM

In that case, using a 64-bit type if available in place of your class becomes a possible source of subtle and annoying errors.


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.

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.


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?



#15 SiCrane   Moderators   -  Reputation: 9662

Like
0Likes
Like

Posted 05 January 2013 - 06:25 PM

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.

#16 Ectara   Crossbones+   -  Reputation: 3054

Like
0Likes
Like

Posted 05 January 2013 - 09:06 PM

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?



#17 SiCrane   Moderators   -  Reputation: 9662

Like
0Likes
Like

Posted 06 January 2013 - 09:00 AM

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.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS