One of the new-style casting operators of C++ (the others are const_cast, static_cast and dynamic_cast). reinterpret_cast<T>(o) is the same as saying (T)o. The result will be of type T. In particular, it always compiles (unlike the other casts) and it always succeeds at runtime (unlike dynamic_cast).

Use reinterpret_cast only when you have no other choice. This is what you use to do the really ugly, "down-to-the-metal" casts in the bowels of particularly hairy code. Use it to do things like cast between an integer and a pointer (implementation defined according to ISO C++, but nonetheless sometimes necessary). Uggh.

The other casting operators have no parallel in ISO C, but reinterpret_cast<T> is (T); why add it?

Three reasons; at least the first two are Bjarne Stroustrup's:

  • It's much uglier, which is fitting for an ugly operation.
  • It's much easier to grep for.
  • It's consistent with the other three predefined syntaxes (and will be consistent with any additional user-defined syntaxes, which might exist e.g. for converting smart pointers).

Log in or register to write something here or to contact authors.