• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Ectara

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

16 posts in this topic

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.

0

Share this post


Link to post
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.

0

Share this post


Link to post
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.

0

Share this post


Link to post
Share on other sites
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;
}
0

Share this post


Link to post
Share on other sites

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
0

Share this post


Link to post
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.
1

Share this post


Link to post
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.


 

0

Share this post


Link to post
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.
0

Share this post


Link to post
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.

0

Share this post


Link to post
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.
0

Share this post


Link to post
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.

0

Share this post


Link to post
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.
0

Share this post


Link to post
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?

0

Share this post


Link to post
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.
0

Share this post


Link to post
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?

0

Share this post


Link to post
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.
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0