If we use reinterpret_cast to bring a pointer to a pointer, an object to a pointer, or a pointer to an object, then the cast is correct.

 int n = 0; char* c = reinterpret_cast<char*>(n); int another_value = reinterpret_cast<int>(c); 

Why doesn't reinterpret_cast allow casting on simple formats? This code is not going to:

 int a = 0; char b = reinterpret_cast<char>(a); 

on Stroustrup:

reinterpret_cast manages conversions between unrelated types, such as integer to pointers or pointers to other (unrelated) pointers.

according to Myers:

reinterpret_cast intended for low-level casts that generate implementation-dependent (i.e., unbearable) results, such as casting a pointer to an int .

So, both the pointer to the pointer, and the pointer to the int , and the int to the pointer are reinterpret_cast .

Alena L. writes in a blog :

reinterpret_cast - the most brash type conversion. Not portable, the result may be incorrect, no checks are done. It is believed that you know better than the compiler how things really are, and he quietly obeys. Cannot be cast one value to another value. Usually used to bring a pointer to a pointer, a pointer to a integer, an integer to a pointer. Can also work with links.

Well, one value to another cannot be cast. Why this opportunity is not filed?

  • So what would this “opportunity” have to do if it were “gash” ??? What result do you expect to receive from such a reinterpret_cast ? - AnT
  • Interest Ask. Here are the approximate expectations from this "opportunity": to create a new object - a clone of the one to which I am addressing, and the rest is similar to bringing to the link. Why a clone is because if you do not create a new object, then the functionality would be identical to the cast by reference. - W_bear

2 answers 2

reinterpret_cast , as the name implies, is intended primarily to perform memory reinterpretation . That is, when you cast a pointer of type T * to a pointer of type U * , it is assumed that you do this in order to access the memory of the original object of type T , like the memory of an object of type U This is the reinterpretation of memory . (Of course, the legality of such access also depends on the mass of extraneous factors.)

reinterpret_cast also supports an analogue of such a pointer conversion, but in the variant with links instead of pointers, i.e. You can do

 T t; U &u = reinterpret_cast<U &>(t); 

continue to access the memory of the object t as an object of type U

Also on reinterpret_cast , the side functionality is additionally “hung” - a cast between pointer and integer types. Everything.

But what you wanted to achieve by performing a reinterpret_cast type int to type char is not clear. If you need re-interpretation of memory, then it is done like this.

 int a = 0; char b = reinterpret_cast<char &>(a); 

that is, the target type must be a reference.

Your variant looks like a normal type conversion performed by static_cast , not reinterpret_cast . The whole idea of ​​the diversity of caste types in C ++ comes down to the division of responsibilities between them with minimal intersections.

  • +1 for reinterpret_cast on links. I did not even think about this. - αλεχολυτ

See it.

Transformations are of two types: changing the internal representation, and not changing it.

Converting int to double changes the internal representation, double has a clever binary layout with a mantissa and order. Converting integer types also changes the internal representation if they have different lengths.

But the meaning of reinterpret_cast , on the contrary, is that it changes the type of the object from the point of view of the compiler , and (except for possible pathological compilers) is not displayed in any way in the object code. Well, you remember that an object is just a set of bits in memory and its type, which allows to interpret it? reinterpret_cast just asks the compiler to interpret it differently, without changing the bits themselves in memory.


And a transformation that can do everything, convert both numeric types and pointers, has already been written. This is a C-style cast . However, due to excessive power, its use is not recommended - precisely because it can convert numbers, pointers, and your cat, even if you don’t want to.

  • "without changing the bits themselves in the memory" - an interesting point - caste the pointer into a number. What if the number of the number does not match the bit of the pointer? - Qwertiy ♦
  • @Qwertiy: Not compiling: ideone.com/GkL1XS - VladD
  • Yes, with int, too . Suddenly. But how then to cast - always in long? After all, any if sizeof not rolled? - Qwertiy ♦
  • @Qwertiy: In size_t ? He seems to be the right size. The problem is further complicated by the fact that different pointers can be of different sizes. - VladD
  • one
    @Qwertiy for pointers is intptr_t and uintptr_t . - αλεχολυτ