Difference between revisions of "cpp/language/reinterpret cast"
(→Notes: CWG2051 is resolved. Moving the paragraph to last, no DR list.) |
(c -> c/core, and applied {{box}}.) |
||
Line 5: | Line 5: | ||
===Syntax=== | ===Syntax=== | ||
− | |||
{{sdsc begin}} | {{sdsc begin}} | ||
− | {{sdsc|{{ttb|reinterpret_cast | + | {{sdsc|{{ttb|reinterpret_cast<}} {{spar|new-type}} {{ttb|>(}} {{spar|expression}} {{ttb|)}}}} |
{{sdsc end}} | {{sdsc end}} | ||
Line 13: | Line 12: | ||
===Explanation=== | ===Explanation=== | ||
− | Unlike {{c|static_cast}}, but like {{c|const_cast}}, the {{c|reinterpret_cast}} expression does not compile to any CPU instructions (except when converting between integers and pointers or on obscure architectures where pointer representation depends on its type). It is purely a compile-time directive which instructs the compiler to treat {{spar|expression}} as if it had the type {{spar|new-type}}. | + | Unlike {{c/core|static_cast}}, but like {{c/core|const_cast}}, the {{c/core|reinterpret_cast}} expression does not compile to any CPU instructions (except when converting between integers and pointers or on obscure architectures where pointer representation depends on its type). It is purely a compile-time directive which instructs the compiler to treat {{spar|expression}} as if it had the type {{spar|new-type}}. |
− | Only the following conversions can be done with {{c|reinterpret_cast}}, except when such conversions would cast away ''constness'' or ''volatility''. | + | Only the following conversions can be done with {{c/core|reinterpret_cast}}, except when such conversions would cast away ''constness'' or ''volatility''. |
− | @1@ An expression of 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 {{spar|expression}}. | + | @1@ An expression of 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 {{spar|expression}}. |
@2@ A pointer can be converted to any integral type large enough to hold all values of its type (e.g. to {{lc|std::uintptr_t}}) | @2@ A pointer can be converted to any integral type large enough to hold all values of its type (e.g. to {{lc|std::uintptr_t}}) | ||
@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 round-trip conversion in the opposite direction is not guaranteed; the same pointer may have multiple integer representations) The null pointer constant {{lc|NULL}} or integer zero is not guaranteed to yield the null pointer value of the target type; {{rlpt|static_cast}} or {{rlp|implicit cast|implicit conversion}} should be used for this purpose. | @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 round-trip conversion in the opposite direction is not guaranteed; the same pointer may have multiple integer representations) The null pointer constant {{lc|NULL}} or integer zero is not guaranteed to yield the null pointer value of the target type; {{rlpt|static_cast}} or {{rlp|implicit cast|implicit conversion}} should be used for this purpose. | ||
{{rrev|since=c++11| | {{rrev|since=c++11| | ||
− | @4@ Any value of type {{lc|std::nullptr_t}}, including {{c|nullptr}} can be converted to any integral type as if it were {{c|(void*)0}}, but no value, not even {{c|nullptr}} can be converted to {{lc|std::nullptr_t}}: {{c|static_cast}} should be used for that purpose. | + | @4@ Any value of type {{lc|std::nullptr_t}}, including {{c|nullptr}} can be converted to any integral type as if it were {{c|(void*)0}}, but no value, not even {{c|nullptr}} can be converted to {{lc|std::nullptr_t}}: {{c/core|static_cast}} should be used for that purpose. |
}} | }} | ||
− | @5@ Any object pointer type {{tt|T1*}} can be converted to another object pointer type {{tt|''cv'' T2*}}. This is exactly equivalent to | + | @5@ Any object pointer type {{tt|T1*}} can be converted to another object pointer type {{tt|''cv'' T2*}}. This is exactly equivalent to {{box|{{c/core|static_cast<}}''cv''{{c/core| T2*>(static_cast<}}''cv''{{c/core| void*>(}}{{spar|expression}}{{tt|))}}}} (which implies that if {{tt|T2}}'s alignment requirement is not stricter than {{tt|T1}}'s, the value of the pointer does not change and conversion of the resulting pointer back to its original type yields the original value). In any case, the resulting pointer may only be dereferenced safely if allowed by the ''type aliasing'' rules (see below){{mark unreviewed dr|cwg}}<!-- cwg1412 --> |
@6@ An {{rev inl|until=c++11|{{rlp|value category#lvalue|lvalue}}}}{{rev inl|since=c++11|{{rlp|value category#glvalue|glvalue}}}} expression of type {{tt|T1}} can be converted to reference to another type {{tt|T2}}. The result is that of {{c|*reinterpret_cast<T2*>(p)}}, where {{c|p}} is a pointer of type “pointer to {{tt|T1}}” to the object designated by {{spar|expression}}. 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 (see below) | @6@ An {{rev inl|until=c++11|{{rlp|value category#lvalue|lvalue}}}}{{rev inl|since=c++11|{{rlp|value category#glvalue|glvalue}}}} expression of type {{tt|T1}} can be converted to reference to another type {{tt|T2}}. The result is that of {{c|*reinterpret_cast<T2*>(p)}}, where {{c|p}} is a pointer of type “pointer to {{tt|T1}}” to the object designated by {{spar|expression}}. 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 (see below) | ||
@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. | @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 (in particular, on any POSIX compatible system as required by [http://pubs.opengroup.org/onlinepubs/9699919799/functions/dlsym.html {{tt|dlsym}}]), a function pointer can be converted to {{c|void*}} or any other 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. | + | @8@ On some implementations (in particular, on any POSIX compatible system as required by [http://pubs.opengroup.org/onlinepubs/9699919799/functions/dlsym.html {{tt|dlsym}}]), a function pointer can be converted to {{c/core|void*}} or any other 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 {{c|nullptr}} or any other value of type {{lc|std::nullptr_t}} cannot be converted to a pointer with {{c|reinterpret_cast}}: implicit conversion or {{c|static_cast}} should be used for this purpose. | + | @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 {{c|nullptr}} or any other value of type {{lc|std::nullptr_t}} cannot be converted to a pointer with {{c/core|reinterpret_cast}}: implicit conversion or {{c/core|static_cast}} should be used for this purpose. |
@10@ A pointer to member function can be converted to pointer to a different member function of a different type. Conversion back to the original type yields the original value, otherwise the resulting pointer cannot be used safely. | @10@ A pointer to member function can be converted to pointer to a different member function of a different type. Conversion back to the original type yields the original value, otherwise the resulting pointer cannot be used safely. | ||
@11@ A pointer to member object of some class {{tt|T1}} can be converted to a pointer to another member object of another class {{tt|T2}}. If {{tt|T2}}'s alignment is not stricter than {{tt|T1}}'s, conversion back to the original type {{tt|T1}} yields the original value, otherwise the resulting pointer cannot be used safely. | @11@ A pointer to member object of some class {{tt|T1}} can be converted to a pointer to another member object of another class {{tt|T2}}. If {{tt|T2}}'s alignment is not stricter than {{tt|T1}}'s, conversion back to the original type {{tt|T1}} yields the original value, otherwise the resulting pointer cannot be used safely. | ||
Line 41: | Line 40: | ||
* {{tt|AliasedType}} and {{tt|DynamicType}} are ''similar''. | * {{tt|AliasedType}} and {{tt|DynamicType}} are ''similar''. | ||
* {{tt|AliasedType}} is the (possibly {{rlp|cv}}-qualified) signed or unsigned variant of {{tt|DynamicType}}. | * {{tt|AliasedType}} is the (possibly {{rlp|cv}}-qualified) signed or unsigned variant of {{tt|DynamicType}}. | ||
− | * {{tt|AliasedType}} is {{rev inl|since=c++17|{{ltt|cpp/types/byte|std::byte}},}} {{c|char}}, or {{c|unsigned char}}: this permits examination of the {{rlp|object#Object representation and value representation|object representation}} of any object as an array of bytes. | + | * {{tt|AliasedType}} is {{rev inl|since=c++17|{{ltt|cpp/types/byte|std::byte}},}} {{c/core|char}}, or {{c/core|unsigned char}}: this permits examination of the {{rlp|object#Object representation and value representation|object representation}} of any object as an array of bytes. |
Informally, two types are ''similar'' if, ignoring top-level cv-qualification: | Informally, two types are ''similar'' if, ignoring top-level cv-qualification: | ||
Line 54: | Line 53: | ||
For example: | For example: | ||
− | *{{c|const int * volatile *}} and {{c|int * * const}} are similar; | + | *{{c/core|const int * volatile *}} and {{c/core|int * * const}} are similar; |
− | *{{c|const int (* volatile S::* const)[20]}} and {{c|int (* const S::* volatile)[20]}} are similar; | + | *{{c/core|const int (* volatile S::* const)[20]}} and {{c/core|int (* const S::* volatile)[20]}} are similar; |
− | *{{c|int (* const *)(int *)}} and {{c|int (* volatile *)(int *)}} are similar; | + | *{{c/core|int (* const *)(int *)}} and {{c/core|int (* volatile *)(int *)}} are similar; |
− | *{{c|int (S::*)() const}} and {{c|int (S::*)()}} are ''not'' similar; | + | *{{c/core|int (S::*)() const}} and {{c/core|int (S::*)()}} are ''not'' similar; |
− | *{{c|int (*)(int *)}} and {{c|int (*)(const int *)}} are ''not'' similar; | + | *{{c/core|int (*)(int *)}} and {{c/core|int (*)(const int *)}} are ''not'' similar; |
− | *{{c|const int (*)(int *)}} and {{c|int (*)(int *)}} are ''not'' similar; | + | *{{c/core|const int (*)(int *)}} and {{c/core|int (*)(int *)}} are ''not'' similar; |
− | *{{c|int (*)(int * const)}} and {{c|int (*)(int *)}} are similar (they are the same type); | + | *{{c/core|int (*)(int * const)}} and {{c/core|int (*)(int *)}} are similar (they are the same type); |
− | *{{c|std::pair<int, int>}} and {{c|std::pair<const int, int>}} are ''not'' similar. | + | *{{c/core|std::pair<int, int>}} and {{c/core|std::pair<const int, int>}} are ''not'' similar. |
This rule enables type-based alias analysis, in which a compiler assumes that the value read through a glvalue of one type is not modified by a write to a glvalue of a different type (subject to the exceptions noted above). | This rule enables type-based alias analysis, in which a compiler assumes that the value read through a glvalue of one type is not modified by a write to a glvalue of a different type (subject to the exceptions noted above). | ||
Line 92: | Line 91: | ||
}} | }} | ||
− | Performing a class member access that designates a non-static data member or a non-static member function on a glvalue that does not actually designate an object of the appropriate type - such as one obtained through a {{ | + | Performing a class member access that designates a non-static data member or a non-static member function on a glvalue that does not actually designate an object of the appropriate type - such as one obtained through a {{c/core|reinterpret_cast}} - results in undefined behavior: |
{{source|1= | {{source|1= | ||
Line 120: | Line 119: | ||
Many compilers issue "strict aliasing" warnings in such cases, even though technically such constructs run afoul of something other than the paragraph commonly known as the "strict aliasing rule". | Many compilers issue "strict aliasing" warnings in such cases, even though technically such constructs run afoul of something other than the paragraph commonly known as the "strict aliasing rule". | ||
− | The purpose of strict aliasing and related rules is to enable type-based alias analysis, which would be decimated if a program can validly create a situation where two pointers to unrelated types (e.g., an {{c|int*}} and a {{c|float*}}) could simultaneously exist and both can be used to load or store the same memory (see [http://www.open-std.org/pipermail/ub/2016-February/000565.html this email on SG12 reflector]). Thus, any technique that is seemingly capable of creating such a situation necessarily invokes undefined behavior. | + | The purpose of strict aliasing and related rules is to enable type-based alias analysis, which would be decimated if a program can validly create a situation where two pointers to unrelated types (e.g., an {{c/core|int*}} and a {{c/core|float*}}) could simultaneously exist and both can be used to load or store the same memory (see [http://www.open-std.org/pipermail/ub/2016-February/000565.html this email on SG12 reflector]). Thus, any technique that is seemingly capable of creating such a situation necessarily invokes undefined behavior. |
− | When it is needed to interpret the bytes of an object as a value of a different type, {{lc|std::memcpy}} {{rev inl|since=c++20|or {{ltt|cpp/numeric/bit_cast|std::bit_cast}} }}can be used: | + | When it is needed to interpret the bytes of an object as a value of a different type, {{lc|std::memcpy}} {{rev inl|since=c++20|or {{ltt|cpp/numeric/bit_cast|std::bit_cast}}}} can be used: |
{{source|1= | {{source|1= | ||
Line 134: | Line 133: | ||
{{rrev|since=c++11| | {{rrev|since=c++11| | ||
− | If the implementation provides {{lc|std::intptr_t}} and/or {{lc|std::uintptr_t}}, then a cast from a pointer to an object type or ''cv'' {{c|void}} to these types is always well-defined. However, this is not guaranteed for a function pointer. | + | If the implementation provides {{lc|std::intptr_t}} and/or {{lc|std::uintptr_t}}, then a cast from a pointer to an object type or ''cv'' {{c/core|void}} to these types is always well-defined. However, this is not guaranteed for a function pointer. |
}} | }} | ||
Line 141: | Line 140: | ||
* {{tt|AliasedType}} is a (possibly {{rlp|cv}}-qualified) {{rlp|derived class|base class}} of {{tt|DynamicType}}. | * {{tt|AliasedType}} is a (possibly {{rlp|cv}}-qualified) {{rlp|derived class|base class}} of {{tt|DynamicType}}. | ||
− | These bullets describe situations that cannot arise in C++ and therefore are omitted from the discussion above. In C, aggregate copy and assignment access the aggregate object as a whole. But in C++ such actions are always performed through a member function call, which accesses the individual subobjects rather than the entire object (or, in the case of unions, copies the object representation, i.e., via {{c|unsigned char}}). These bullets were eventually removed via {{ | + | These bullets describe situations that cannot arise in C++ and therefore are omitted from the discussion above. In C, aggregate copy and assignment access the aggregate object as a whole. But in C++ such actions are always performed through a member function call, which accesses the individual subobjects rather than the entire object (or, in the case of unions, copies the object representation, i.e., via {{c/core|unsigned char}}). These bullets were eventually removed via {{cwg|2051}}. |
===Example=== | ===Example=== | ||
− | |||
{{example | {{example | ||
− | |Demonstrates some uses of {{ | + | |Demonstrates some uses of {{c/core|reinterpret_cast}}: |
|code= | |code= | ||
#include <cstdint> | #include <cstdint> | ||
Line 157: | Line 155: | ||
{ | { | ||
int i = 7; | int i = 7; | ||
− | + | ||
// pointer to integer and back | // pointer to integer and back | ||
std::uintptr_t v1 = reinterpret_cast<std::uintptr_t>(&i); // static_cast is an error | std::uintptr_t v1 = reinterpret_cast<std::uintptr_t>(&i); // static_cast is an error | ||
Line 163: | Line 161: | ||
int* p1 = reinterpret_cast<int*>(v1); | int* p1 = reinterpret_cast<int*>(v1); | ||
assert(p1 == &i); | assert(p1 == &i); | ||
− | + | ||
// pointer to function to another and back | // pointer to function to another and back | ||
void(*fp1)() = reinterpret_cast<void(*)()>(f); | void(*fp1)() = reinterpret_cast<void(*)()>(f); | ||
Line 169: | Line 167: | ||
int(*fp2)() = reinterpret_cast<int(*)()>(fp1); | int(*fp2)() = reinterpret_cast<int(*)()>(fp1); | ||
std::cout << std::dec << fp2() << '\n'; // safe | std::cout << std::dec << fp2() << '\n'; // safe | ||
− | + | ||
// type aliasing through pointer | // type aliasing through pointer | ||
char* p2 = reinterpret_cast<char*>(&i); | char* p2 = reinterpret_cast<char*>(&i); | ||
std::cout << (p2[0] == '\x7' ? "This system is little-endian\n" | std::cout << (p2[0] == '\x7' ? "This system is little-endian\n" | ||
: "This system is big-endian\n"); | : "This system is big-endian\n"); | ||
− | + | ||
// type aliasing through reference | // type aliasing through reference | ||
reinterpret_cast<unsigned int&>(i) = 42; | reinterpret_cast<unsigned int&>(i) = 42; | ||
std::cout << i << '\n'; | std::cout << i << '\n'; | ||
− | + | ||
[[maybe_unused]] const int &const_iref = i; | [[maybe_unused]] const int &const_iref = i; | ||
// int &iref = reinterpret_cast<int&>( | // int &iref = reinterpret_cast<int&>( | ||
Line 197: | Line 195: | ||
{{dr list item|wg=cwg|dr=658|std=C++98|before=the result of pointer conversions was unspecified<br>(except for conversions back to the original type)|after=specification provided for pointers<br>whose pointed-to types satisfy<br>the alignment requirements}} | {{dr list item|wg=cwg|dr=658|std=C++98|before=the result of pointer conversions was unspecified<br>(except for conversions back to the original type)|after=specification provided for pointers<br>whose pointed-to types satisfy<br>the alignment requirements}} | ||
{{dr list item|wg=cwg|dr=799|std=C++98|before=it was unclear which identity conversion<br>can be done by {{tt|reinterpret_cast}}|after=made clear}} | {{dr list item|wg=cwg|dr=799|std=C++98|before=it was unclear which identity conversion<br>can be done by {{tt|reinterpret_cast}}|after=made clear}} | ||
− | {{dr list item|wg=cwg|dr=1268|std=C++11|before={{ | + | {{dr list item|wg=cwg|dr=1268|std=C++11|before={{c/core|reinterpret_cast}} could only cast lvalues to reference types|after=xvalues also allowed}} |
{{dr list end}} | {{dr list end}} | ||
Revision as of 18:51, 12 February 2023
Converts between types by reinterpreting the underlying bit pattern.
Contents |
Syntax
reinterpret_cast< new-type >( expression )
|
|||||||||
Returns a value of type new-type.
Explanation
Unlike static_cast, but like const_cast, the reinterpret_cast expression does not compile to any CPU instructions (except when converting between integers and pointers or on obscure architectures where pointer representation depends on its type). It is purely a compile-time directive which instructs the compiler to treat expression as if it had the type new-type.
Only the following conversions can be done with reinterpret_cast, except when such conversions would cast away constness or volatility.
static_cast
or implicit conversion should 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 were (void*)0, but no value, not even nullptr can be converted to std::nullptr_t: static_cast should be used for that purpose.
|
(since C++11) |
T1*
can be converted to another object pointer type cv T2*
. This is exactly equivalent to static_cast<cv T2*>(static_cast<cv void*>(expression))
(which implies that if T2
's alignment requirement is not stricter than T1
's, the value of the pointer does not change and conversion of the resulting pointer back to its original type yields the original value). In any case, the resulting pointer may only be dereferenced safely if allowed by the type aliasing rules (see below)T1
can be converted to reference to another type T2
. The result is that of *reinterpret_cast<T2*>(p), where p is a pointer of type “pointer to T1
” to the object designated by expression. 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 (see below)dlsym
), a function pointer can be converted to void* or any other 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.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 back to the original type T1
yields the original value, otherwise the resulting pointer cannot be used safely.As with all cast expressions, the result is:
- an lvalue if target-type is an lvalue reference type or an rvalue reference to function type(since C++11);
|
(since C++11) |
- a prvalue otherwise.
Keywords
Type aliasing
Whenever an attempt is made to read or modify the stored value of an object of type DynamicType
through a glvalue of type AliasedType
, the behavior is undefined unless one of the following is true:
-
AliasedType
andDynamicType
are similar. -
AliasedType
is the (possibly cv-qualified) signed or unsigned variant ofDynamicType
. -
AliasedType
is std::byte,(since C++17) char, or unsigned char: this permits examination of the object representation of any object as an array of bytes.
Informally, two types are similar if, ignoring top-level cv-qualification:
- they are the same type; or
- they are both pointers, and the pointed-to types are similar; or
- they are both pointers to member of the same class, and the types of the pointed-to members are similar; or
|
(until C++20) |
|
(since C++20) |
For example:
- const int * volatile * and int * * const are similar;
- const int (* volatile S::* const)[20] and int (* const S::* volatile)[20] are similar;
- int (* const *)(int *) and int (* volatile *)(int *) are similar;
- int (S::*)() const and int (S::*)() are not similar;
- int (*)(int *) and int (*)(const int *) are not similar;
- const int (*)(int *) and int (*)(int *) are not similar;
- int (*)(int * const) and int (*)(int *) are similar (they are the same type);
- std::pair<int, int> and std::pair<const int, int> are not similar.
This rule enables type-based alias analysis, in which a compiler assumes that the value read through a glvalue of one type is not modified by a write to a glvalue of a different type (subject to the exceptions noted above).
Note that many C++ compilers relax this rule, as a non-standard language extension, to allow wrong-type access through the inactive member of a union (such access is not undefined in C).
Notes
Assuming that alignment requirements are met, a reinterpret_cast
does not change the value of a pointer outside of a few limited cases dealing with pointer-interconvertible objects:
struct S1 { int a; } s1; struct S2 { int a; private: int b; } s2; // not standard-layout union U { int a; double b; } u = {0}; int arr[2]; int* p1 = reinterpret_cast<int*>(&s1); // value of p1 is "pointer to s1.a" because // s1.a and s1 are pointer-interconvertible int* p2 = reinterpret_cast<int*>(&s2); // value of p2 is unchanged by reinterpret_cast // and is "pointer to s2". int* p3 = reinterpret_cast<int*>(&u); // value of p3 is "pointer to u.a": // u.a and u are pointer-interconvertible double* p4 = reinterpret_cast<double*>(p3); // value of p4 is "pointer to u.b": u.a and // u.b are pointer-interconvertible because // both are pointer-interconvertible with u int* p5 = reinterpret_cast<int*>(&arr); // value of p5 is unchanged by reinterpret_cast // and is "pointer to arr"
Performing a class member access that designates a non-static data member or a non-static member function on a glvalue that does not actually designate an object of the appropriate type - such as one obtained through a reinterpret_cast - results in undefined behavior:
struct S { int x; }; struct T { int x; int f(); }; struct S1 : S {}; // standard-layout struct ST : S, T {}; // not standard-layout S s = {}; auto p = reinterpret_cast<T*>(&s); // value of p is "pointer to s" auto i = p->x; // class member access expression is undefined behavior; // s is not a T object p->x = 1; // undefined behavior p->f(); // undefined behavior S1 s1 = {}; auto p1 = reinterpret_cast<S*>(&s1); // value of p1 is "pointer to the S subobject of s1" auto i = p1->x; // OK p1->x = 1; // OK ST st = {}; auto p2 = reinterpret_cast<S*>(&st); // value of p2 is "pointer to st" auto i = p2->x; // undefined behavior p2->x = 1; // undefined behavior
Many compilers issue "strict aliasing" warnings in such cases, even though technically such constructs run afoul of something other than the paragraph commonly known as the "strict aliasing rule".
The purpose of strict aliasing and related rules is to enable type-based alias analysis, which would be decimated if a program can validly create a situation where two pointers to unrelated types (e.g., an int* and a float*) could simultaneously exist and both can be used to load or store the same memory (see this email on SG12 reflector). Thus, any technique that is seemingly capable of creating such a situation necessarily invokes undefined behavior.
When it is needed to interpret the bytes of an object as a value of a different type, std::memcpy or std::bit_cast(since C++20) can be used:
double d = 0.1; std::int64_t n; static_assert(sizeof n == sizeof d); // n = *reinterpret_cast<std::int64_t*>(&d); // Undefined behavior std::memcpy(&n, &d, sizeof d); // OK n = std::bit_cast<std::int64_t>(d); // also OK
If the implementation provides std::intptr_t and/or std::uintptr_t, then a cast from a pointer to an object type or cv void to these types is always well-defined. However, this is not guaranteed for a function pointer. |
(since C++11) |
The paragraph defining the strict aliasing rule in the standard used to contain two additional bullets partially inherited from C:
-
AliasedType
is an aggregate type or a union type which holds one of the aforementioned types as an element or non-static member (including, recursively, elements of subaggregates and non-static data members of the contained unions). -
AliasedType
is a (possibly cv-qualified) base class ofDynamicType
.
These bullets describe situations that cannot arise in C++ and therefore are omitted from the discussion above. In C, aggregate copy and assignment access the aggregate object as a whole. But in C++ such actions are always performed through a member function call, which accesses the individual subobjects rather than the entire object (or, in the case of unions, copies the object representation, i.e., via unsigned char). These bullets were eventually removed via CWG issue 2051.
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 std::uintptr_t v1 = reinterpret_cast<std::uintptr_t>(&i); // static_cast is an error std::cout << "The value of &i is " << std::showbase << 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); std::cout << (p2[0] == '\x7' ? "This system is little-endian\n" : "This system is big-endian\n"); // type aliasing through reference reinterpret_cast<unsigned int&>(i) = 42; std::cout << i << '\n'; [[maybe_unused]] const int &const_iref = i; // int &iref = reinterpret_cast<int&>( // const_iref); // compiler error - can't get rid of const // Must use const_cast instead: int &iref = const_cast<int&>(const_iref); }
Possible output:
The value of &i is 0x7fff352c3580 42 This system is little-endian 42
Defect reports
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR | Applied to | Behavior as published | Correct behavior |
---|---|---|---|
CWG 195 | C++98 | conversion between function pointers and object pointers not allowed |
made conditionally-supported |
CWG 658 | C++98 | the result of pointer conversions was unspecified (except for conversions back to the original type) |
specification provided for pointers whose pointed-to types satisfy the alignment requirements |
CWG 799 | C++98 | it was unclear which identity conversion can be done by reinterpret_cast
|
made clear |
CWG 1268 | C++11 | reinterpret_cast could only cast lvalues to reference types | xvalues also allowed |
See also
const_cast conversion
|
adds or removes const |
static_cast conversion
|
performs basic conversions |
dynamic_cast conversion
|
performs checked polymorphic conversions |
explicit casts | permissive conversions between types |
standard conversions | implicit conversions from one type to another |
(C++20) |
reinterpret the object representation of one type as that of another (function template) |