Home c++ What is the meaning of reinterpret_cast's existence?

What is the meaning of reinterpret_cast’s existence?

Author

Date

Category

In C++, there is a reinterpret_cast operator, the meaning of which is to cast between types that are incompatible with each other.

However, such transformations violate strict aliasing rule , which provokes undefined behavior. The same conversions that do not violate this rule fit into const_cast , static_cast and dynamic_cast .

What then is the raison d’être of this operator if its use violates the standard?


Answer 1, authority 100%

reinterpret_cast is used for more than just converting pointers from one type to another. There are several different transformations. cppreference.com highlights 11 conversion options:

  1. Into your own type
  2. Pointer to integral type
  3. Integral type to pointer
  4. of type std :: nullptr_t to integral type
  5. Pointer of one type to pointer of another type
  6. lvalue of one type to reference to another type
  7. A pointer to a function of one type to a pointer to a function of another type
  8. Function pointer in void *
  9. A null pointer of any type to a pointer of any other type
  10. rvalue pointer of one type to member function to pointer of another type to member function
  11. rvalue pointer of a data member of one type to a pointer to another data member of a different type

The

Type aliasing rules only affect clauses 5 and 6 and the result can be safely used (i.e. without violating strict-aliasing ) in the following cases:

  • The resulting type is the dynamic type of the original object
  • Result type and dynamic type point to the same type T
  • The resulting type is a signed or unsigned variant of the source object type
  • The resulting type is an aggregate type or union that contains an element or non-static data member used as the source object. Those. you can get a pointer to a structure by a pointer to its member.
  • The resulting type is the base class of the dynamic type of the source object and this type is the standard-layout class and does not contain non-static data members, and the resulting type is the first base class.
  • The resulting type is a pointer to char , unsigned char , or std :: byte .

Some implementations relax these rules as non-standard language extensions.


Answer 2, authority 36%

There is exactly one type of conversion between incompatible types that does not violate the strict aliasing rule – from an arbitrary pointer to a pointer of type char * . That is, reinterpret_cast allows you to represent an arbitrary object as a sequence of bytes (since the standard guarantees a single-byte length of char ).

Here’s an example of how to use this kind of transformation wisely:

template & lt; class T & gt;
void putIntoStream (const T * object, std :: ostream & amp; out)
{
  out.write (reinterpret_cast & lt; const char * & gt; (object), sizeof (T));
}

For everything else, there is memcpy () .


Answer 3, authority 18%

Although reinterpret_cast leads to undefined behavior in most cases, libraries can use it in their implementations by testing them against specific compilers. For the library user, the behavior will no longer be undefined, since it is tested and documented, but it will have to consider the list of supported compilers. Sometimes this is the only way to develop a cross-platform library.

In addition, there are still cases when you have to sacrifice portability to achieve other goals (in particular, this is relevant for microcontrollers).

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions