473,398 Members | 2,088 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

reinterpret_cast and enumerations

enum foo {a,b,c,d};

int main()
{
unsigned int bar=reinterpret_cast< unsigned int >( a );
return bar;
}

g++ rejects the code based on the reinterpret_cast, so I presume it is
not legal. Why not? And why should g++ accept the code when
static_cast is used instead (as it does)?

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Jul 22 '05 #1
5 3989
Christopher Benson-Manica wrote:
enum foo {a,b,c,d};

int main()
{
unsigned int bar=reinterpret_cast< unsigned int >( a );
return bar;
}

g++ rejects the code based on the reinterpret_cast, so I presume it is
not legal. Why not?
You will have to ask in comp.std.c++. The Standard lists conversions that
are allowed to be performed by reinterpret_cast and all others are not
allowed. Why that is so, is not explained in the Standard, and here, in
comp.lang.c++ the answer to your 'why' question is "because the Standard
says so".
And why should g++ accept the code when
static_cast is used instead (as it does)?


Because static_cast has different set of restrictions and they are not the
same as reinterpret_cast.

V
Jul 22 '05 #2
On Tue, 2 Nov 2004 17:19:33 +0000 (UTC), Christopher Benson-Manica
<at***@nospam.cyberspace.org> wrote:
enum foo {a,b,c,d};

int main()
{
unsigned int bar=reinterpret_cast< unsigned int >( a );
return bar;
}

g++ rejects the code based on the reinterpret_cast, so I presume it is
not legal. Why not? And why should g++ accept the code when
static_cast is used instead (as it does)?


reinterpret_cast can't perform conversions between integral types,
that's what static_cast is for. The point of the new C++ style casts
is to provide different casts for different jobs.

Tom
Jul 22 '05 #3
On Tue, 2 Nov 2004 17:19:33 +0000 (UTC), Christopher Benson-Manica
<at***@nospam.cyberspace.org> wrote:
enum foo {a,b,c,d};

int main()
{
unsigned int bar=reinterpret_cast< unsigned int >( a );
return bar;
}

g++ rejects the code based on the reinterpret_cast, so I presume it is
not legal. Why not? And why should g++ accept the code when
static_cast is used instead (as it does)?


It is totally unnecessary to cast here since the conversion is done
automatically. Only to avoid comparing signed and unsigned values
should there be a need to cast.

In general, static_cast is used to cast between value types;
reinterpret_cast is usually used for casting unrelated types. Others
have pointed out that the standard defines different rules for the two
kinds of cast.

--
Bob Hairgrove
No**********@Home.com
Jul 22 '05 #4
"Bob Hairgrove" <in*****@bigfoot.com> wrote in message
In general, static_cast is used to cast between value types;
reinterpret_cast is usually used for casting unrelated types. Others
have pointed out that the standard defines different rules for the two
kinds of cast.


Is it true to say that reinterpret_cast is used to cast between unrelated
pointer/reference types. Whereas static_cast is used to cast between
integer types, between float types, between parent/child pointers.
Jul 22 '05 #5
Bob Hairgrove wrote:
In general, static_cast is used to cast between value types;
reinterpret_cast is usually used for casting unrelated types. Others
have pointed out that the standard defines different rules for the two
kinds of cast.


My way of describing static_cast is that it supports two functions:
1. Forcing a conversion that could happen anyway:
double d = 4.5;
cout << d - static_cast<int>(d);
the double->int conversion is defined, so I'm just making it happen
explciitly.

2. Inverting a conversion that I am certain is valid.
struct Base { };
struct Derived : Base { };
Derived d;
Base* bp = &d; // defined implicit conversion.
Derived* dp = static_cast<Derived*>(dp); // well-behaved inverse of defined conversion
Jul 22 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: Suzanne Vogel | last post by:
I'd like to convert a double to a binary representation. I can use the "&" bit operation with a bit mask to convert *non* float types to binary representations, but I can't use "&" on doubles. ...
0
by: Plinkerton | last post by:
I'm making an Base Class that will be inherited. In my base class, I have a public enumeration that defines a list of things I want my class to be able to do. I use it for Method input...
1
by: someone else | last post by:
I have some code that creates dynamic enumerations for use in a PropertyGrid control. This all works perfectly but the memory usage of the program increases quite quicly when viewing the...
4
by: ChrisB | last post by:
Hello: I will be creating 50+ enumerations related to a large number of classes that span a number of namespaces. I was wondering if there are any "best practices" when defining enumerations. ...
27
by: Ben Finney | last post by:
Antoon Pardon wrote: > I just downloaded your enum module for python > and played a bit with it. IMO some of the behaviour makes it less > usefull. Feedback is appreciated. I'm hoping to...
77
by: Ben Finney | last post by:
Howdy all, PEP 354: Enumerations in Python has been accepted as a draft PEP. The current version can be viewed online: <URL:http://www.python.org/peps/pep-0354.html> Here is the...
2
by: Taran | last post by:
Hi All, I was trying some code which essentially does what the function 'func' here does. To simplify the things and make a consise post I have created a dummy app to show my problem. The issue...
2
by: jasm | last post by:
hello everybody! I have used reinterpret_cast for interpret a class object as a char*. This is the object: template<class T> class Coordinates { public: T *x; T *y;
2
by: ciccio | last post by:
Hi, I was wondering what the main reason is why reinterpret_cast fails to work as expected when using optimizations. Here is the simple example code which fail to give the correct result when...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.