reinterpret_cast conversion

From Cppreference

Jump to: navigation, search

Converts between unrelated types.

Contents

[edit] Syntax

reinterpret_cast < new_type > ( expression )

Returns an value of type new_type.

[edit] Explanation

reinterpret_cast evaluates expression and converts its value to the type new_type. If new_type is an lvalue reference or an rvalue reference to function, the result is an lvalue. If new_type is an rvalue reference to object, the result is an xvalue. Otherwise, the result is a prvalue and lvalue-to-rvalue, array-to-pointer, or function-to-pointer implicit conversion is performed if necessary.

Only the following conversions can be done with reinrepret_cast, except when such conversion would cast away constness or volatility.

1. An expression if integral, enumeration, pointer, or pointer-to-member type can be converted to its own type. The resulting value is the same as the value of expression. (C++11 feature)

2. Any pointer can be converted to any integral type large enough to hold the value of the pointer.

3. A value of any integral or enumeration type can be converted to a pointer type. A pointer converted to an integer of sufficient size and back to the same pointer type is guaranteed to have its original value, otherwise the resulting pointer cannot be dereferenced safely. The null pointer constant NULL or integer zero is not guaranteed to yield the null pointer value of the target type; static_cast or implicit conversion can be used for this purpose.

4. Any value of type std::nullptr_t, including nullptr can be converted to any integral type as if it was (void*)0, but no value, not even nullptr can be converted to std::nullptr_t: static_cast may be used for that purpose. (C++11 feature)

5. Any pointer to object of type T1 can be converted to pointer to object of another type T2. If T2's alignment is not stricter than T1's, conversion of the resulting pointer back to its original type yields the original value, otherwise the resulting pointer cannot be dereferenced safely, except where allowed by the type aliasing rules.

6. An lvalue expression of type T1 can be converted to reference to another type T2. The result is an lvalue or xvalue referring to the same object as the original lvalue, but with a different type. No temporary is created, no copy is made, no constructors or conversion functions are called. The resulting reference can only be accessed safely if allowed by the type aliasing rules.

7. Any pointer to function can be converted to a pointer to a different function type. Calling the function through a pointer to a different function type is undefined, but converting such pointer back to pointer to the original function type yields the pointer to the original function.

8. On some implementations, a function pointer can be converted to an object pointer or vice versa. If the implementation supports conversion in both directions, conversion to the original type yields the original value, otherwise the resulting pointer cannot be dereferenced or called safely.

9. The null pointer value of any pointer type can be converted to any other pointer type, resulting in the null pointer value of that type. Note that the null pointer constant nullptr or any other value of type std::nullptr_t cannot be converted to a pointer: implicit conversion or static_cast can be used for this purpose.

10. An rvalue pointer to member function can be converted to pointer to a different member function of a different type. Conversion to the original type yields the original value, otherwise the resulting pointer cannot be used safely.

11. An rvalue pointer to member object of some class T1 can be converted to a pointer to another member object of another class T2. If T2's alignment is not stricter than T1's, conversion to the original type yields the original value, otherwise the resulting pointer cannot be used safely.

[edit] Keywords

reinterpret_cast

[edit] Example

Demonstrates some uses of reinterpret_cast

#include <cstdint>
#include <cassert>
#include <iostream>
int f() { return 42; }
int main()
{
    int i = 7;
 
    // pointer to integer and back
    uintptr_t v1 = reinterpret_cast<uintptr_t>(&i); // static_cast is an error
    std::cout << "The value of &i is 0x" << std::hex << v1 << '\n';
    int* p1 = reinterpret_cast<int*>(v1);
    assert(p1 == &i);
 
    // pointer to function to another and back
    void(*fp1)() = reinterpret_cast<void(*)()>(f);
    // fp1(); undefined behavior
    int(*fp2)() = reinterpret_cast<int(*)()>(fp1);
    std::cout << std::dec << fp2() << '\n'; // safe
 
    // type aliasing through pointer
    char* p2 = reinterpret_cast<char*>(&i);
    if(p2[0] == '\x7')
        std::cout << "This system is little-endian\n";
    else
        std::cout << "This system is big-endian\n";
 
    // type aliasing through reference
    reinterpret_cast<unsigned int&>(i) = 42;
    std::cout << i << '\n';
}

Output:

The value of &i is 0x7fff352c3580
42
This system is little-endian
42

[edit] See also