reinterpret_cast and enumerations

  • Thread starter Christopher Benson-Manica
  • Start date
C

Christopher Benson-Manica

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)?
 
V

Victor Bazarov

Christopher said:
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
 
T

Tom Widmer

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
 
B

Bob Hairgrove

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

Siemel Naran

Bob Hairgrove said:
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.
 
R

Ron Natalie

Bob said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,996
Messages
2,570,238
Members
46,826
Latest member
robinsontor

Latest Threads

Top