Type casting

A

adamrobillard

unsigned char Value1 = 200;
int Value2 = reinterpret_cast<int>(Value1);

Say I want to do something like this. From the C++ standards I am
reading, you should use the reinterpret_cast when casting between
unrelated pointers and integral types.

This line will give me a compiler error. If I use the <static_cast>
instead it works, I get no errors but from the docs it appears that
this is only valid for casting between members of the same class
hierarchy.

Am I missing something from the documentation? Should I be creating a
pointer to an int and then use the reinterpret_cast instread?
 
V

Victor Bazarov

unsigned char Value1 = 200;
int Value2 = reinterpret_cast<int>(Value1);

You don't need a cast there, most likely.
Say I want to do something like this. From the C++ standards I am
reading, you should use the reinterpret_cast when casting between
unrelated pointers and integral types.

Between unrelated pointers and between pointers and integral types, not
between two values of integral types.
This line will give me a compiler error. If I use the <static_cast>
instead it works, I get no errors but from the docs it appears that
this is only valid for casting between members of the same class
hierarchy.

No. static_cast is used where the reverse conversion is implicit. In the
case in question, in most situations, the cast is unnecessary at all.
Am I missing something from the documentation? Should I be creating a
pointer to an int and then use the reinterpret_cast instread?

No. You should drop the cast. And find a better source of information.

V
 
A

Alf P. Steinbach

* (e-mail address removed):
unsigned char Value1 = 200;
int Value2 = reinterpret_cast<int>(Value1);

Say I want to do something like this. From the C++ standards I am
reading, you should use the reinterpret_cast when casting between
unrelated pointers and integral types.

This line will give me a compiler error. If I use the <static_cast>
instead it works, I get no errors but from the docs it appears that
this is only valid for casting between members of the same class
hierarchy.

Am I missing something from the documentation? Should I be creating a
pointer to an int and then use the reinterpret_cast instread?

With most compilers you don't need a cast at all, because with most
compilers sizeof(int)>1 so that all unsigned char values are
representable by int (so you get an implicit conversion).

A bit imprecise but I think useful way to look at these casts:

reinterpret_cast Keep the bitvalues and size exactly as they are,
just change how they are interpreted.

static_cast Change the bitvalues and/or size, so that the
conceptual value is the same with the new type.

However, with class types a static_cast can perform user-defined
conversions that do anything, where the result's conceptual value is not
necessarily the same as the original.

In the example code you presented you have different sizes, so
reinterpret_cast is not applicable.

Generally you should not use casts: each and every cast is a potential
source of bugs.
 
N

Neelesh Bodas

unsigned char Value1 = 200;
int Value2 = reinterpret_cast<int>(Value1);

4.7p1 , p2
1) An rvalue of an integer type can be converted to an rvalue of
another integer type. An rvalue of an enumeration type can be converted
to an rvalue of an integer type.
2) If the destination type is unsigned, the resulting value is the
least unsigned integer congruent to the source integer

(The term integer type is defined in 3.9.1 p2)

Hence no cast is necessary.
 

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

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top