Name
reinterpret_cast operator — Cast for unsafe pointer conversions
Synopsis
postfix-expr := reinterpret_cast < type-id > ( expression )
The reinterpret_cast
operator
performs potentially unsafe type casts. It is most often used to cast
a pointer to a different pointer type. Casting a pointer to a
different pointer and back is usually safe and yields the original
value. The limitations are that object pointers can be cast only to
object pointers with similar or stricter alignment requirements, and
function pointers can be cast only to function pointers.
Pointers-to-members can be cast only to pointers-to-members. You can
cast an integer to a pointer and vice versa. In all cases, the
destination pointer must be large enough to hold the result. Casting a
null pointer results in a null pointer of the destination type. Any
other cast results in undefined behavior.
Example
template<typename T>
unsigned long addressof(const T& obj)
{
returnreinterpret_cast
<unsigned long>(&obj);
}
See Also
const_cast
, dynamic_cast
, expression, static_cast
, Chapter 3
Get C++ In a Nutshell now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.