6

In the C++ Without Fear: A Beginner's Guide That Makes You Feel Smart book, and in chapter (8), it mentions the following about reinterpret_cast

....converts from one pointer type (int) to another (char*). Because the cast changes the way the data pointed to is interpreted, it is called reinterpret_cast, as opposed to static_cast.*

Can you describe this paragraph here? Especially the reason for the way the operation is named?

Thanks.

5
  • 3
    That's a great name for a book! Commented Jan 20, 2011 at 14:24
  • Try reading this article: informit.com/guides/content.aspx?g=cplusplus&seqNum=134 Commented Jan 20, 2011 at 14:28
  • There was another question today that came out of this book, and it ended up doing anything but make the OP feel smart. Commented Jan 20, 2011 at 14:32
  • Ah, hah, it was you. stackoverflow.com/questions/4746980/c-quitting-a-program Shame you opted not to take my advice about getting a real one. Commented Jan 20, 2011 at 14:32
  • 1
    @Joe: It's a name I have never heard of. Maybe I'm to conservative here, but if it's not recommended on The Definitive C++ Book Guide and List, I'm a skeptic. C++ is a very complex beast, complex enough that there's hundreds of books out there which get the basic facts wrong. I wouldn't suggest any C++ book that isn't recommended by the (experienced part of the) community. Commented Jan 20, 2011 at 14:53

2 Answers 2

9

Basically, reinterpret_cast reinterprets the bit pattern at a specific location as a different type.

See for example here: http://publib.boulder.ibm.com/infocenter/lnxpcomp/v7v91/index.jsp?topic=%2Fcom.ibm.vacpp7l.doc%2Flanguage%2Fref%2Fclrc05keyword_reinterpret_cast.htm

The reinterpret_cast operator produces a value of a new type that has the same bit pattern as its argument.

A static cast converts the argument instead of just reinterpreting it. You can try this out by static_casting an int to float and reinterpret_casting an int to float. The result will be totally different.

Sign up to request clarification or add additional context in comments.

1 Comment

It doesn't necessarily have the same bit pattern. There are very few guarantees on reinterpret_cast in the Standard. 5.2.10/3 says "The mapping performed by reinterpret_cast is implementation-defined. [Note: it might, or might not, produce a representation different from the original value.]" Also, a reinterpret_cast cannot cast an int to a float (it can cast int * to float *). Any implementation that does that cast is in violation of the Standard (5.2.10/1, last sentence), although it's probably a common enough extension.
4

There's nothing fancy here. it's really just intended to reinterpret something.

From standard 5.3.10, reinterpret_cast is aimed to cater the following cases:

  • A pointer can be explicitly converted to any integral type large enough to hold it.
  • A value of integral type or enumeration type can be explicitly converted to a pointer.
  • A pointer to a function can be explicitly converted to a pointer to a function of a different type.
  • A pointer to an object can be explicitly converted to a pointer to a different object type.
  • Converting a pointer to a function into a pointer to an object type or vice versa is conditionally-supported.
  • The null pointer value (4.10) is converted to the null pointer value of the destination type.
  • A prvalue of type “pointer to member of X of type T1” can be explicitly converted to a prvalue of a different type “pointer to member of Y of type T2” if T1 and T2 are both function types or both object types.
  • An lvalue expression of type T1 can be cast to the type “reference to T2” if an expression of type “pointer to T1” can be explicitly converted to the type “pointer to T2” using a reinterpret_cast. That is, a reference cast reinterpret_cast < T& >(x) has the same effect as the conversion *reinterpret_cast< T* >(&x) with the built-in & and * operators (and similarly for reinterpret_cast< T&& >(x)).

2 Comments

You made a mistake - it's paragraph 5.2.10 in standard (you wrote 5.3.10)
Aren't the numbers allowed to change from revision to revision, btw? :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.