reinterpret_cast conversion
Aus cppreference.com
![]() | This page has been machine-translated from the English version of the wiki using Google Translate. The translation may contain errors and awkward wording. Hover over text to see the original version. You can help to fix errors and improve the translation. For instructions click here. |
Konvertiert zwischen verschiedenen Arten von Neuinterpretation der zugrunde liegenden Bit-Muster .
Original:
Converts between types by reinterpreting the underlying bit pattern.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
Inhaltsverzeichnis |
[Bearbeiten]Syntax
reinterpret_cast < new_type> ( expression) | |||||||||
Gibt einen Wert vom Typ
new_type
.Original:
Returns a value of type
new_type
.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
[Bearbeiten]Erklärung
Im Gegensatz zu static_cast, aber wie const_cast, wird die reinterpret_cast Ausdruck zu keinen CPU-Anweisungen kompilieren. Es ist eine reine Compiler-Direktive, die den Compiler, um die Sequenz von Bits (Objekt-Repräsentation) von expression zu behandeln, als ob es die Art new_type hatten weist .
Original:
Unlike static_cast, but like const_cast, the reinterpret_cast expression does not compile to any CPU instructions. It is purely a compiler directive which instructs the compiler to treat the sequence of bits (object representation) of expression as if it had the type new_type.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
Nur die folgenden Konvertierungen können mit reintepret_cast getan werden, außer wenn solche Umwandlungen würde werfen Konstantheit oder Volatilität .
Original:
Only the following conversions can be done with reintepret_cast, except when such conversions would cast away constness or volatility.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
1)
Ein Ausdruck der integralen, Aufzählung, Zeiger oder Zeiger-auf-Member-Typ kann seinen eigenen Typ konvertiert werden. Der resultierende Wert ist der gleiche wie der Wert des
expression
. (seit C++11)Original:
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
expression
. (seit C++11)The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
2)
Jeder Zeiger kann auf jedem integralen Typ, der groß genug ist, um den Wert des Zeigers (zB um std::uintptr_t) halten umgewandelt werden
Original:
Any pointer can be converted to any integral type large enough to hold the value of the pointer (e.g. to std::uintptr_t)
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
3)
Ein Wert beliebigen integraler oder Aufzählungstyp kann in einen Zeiger umgewandelt werden. Ein Zeiger umgewandelt in eine ganze Zahl von ausreichender Größe und zurück zu der gleichen Zeigertyp ist gewährleistet auf seinen ursprünglichen Wert sein, da ansonsten die resultierende Zeiger nicht sicher aufgelöst werden. Die Null-Pointer-Konstante NULL oder ganze Zahl Null ist nicht garantiert, dass die Null-Zeiger-Wert der Zieltyp ergeben; static_cast oder <div class="t-tr-text">implizite Konvertierung
sollte für diesen Zweck verwendet werden .Original:
implicit conversion
The text has been machine-translated via [http://translate.google.com Google Translate].
You can help to correct and verify the translation. Click [http://en.cppreference.com/w/Cppreference:MachineTranslations here] for instructions.
You can help to correct and verify the translation. Click [http://en.cppreference.com/w/Cppreference:MachineTranslations here] for instructions.
Original:
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
implizite Konvertierung</div> should be used for this purpose.
Original:
implicit conversion
The text has been machine-translated via [http://translate.google.com Google Translate].
You can help to correct and verify the translation. Click [http://en.cppreference.com/w/Cppreference:MachineTranslations here] for instructions.
You can help to correct and verify the translation. Click [http://en.cppreference.com/w/Cppreference:MachineTranslations here] for instructions.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
4)
Jeder Wert des Typs std::nullptr_t, einschließlich nullptr kann auf einen beliebigen ganzzahligen Typ konvertiert werden, als ob es (void*)0 war, aber kein Wert, nicht einmal nullptr können std::nullptr_t umgewandelt werden: static_cast sollte zu diesem Zweck verwendet werden. (seit C++11)
Original:
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 should be used for that purpose. (seit C++11)
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
5)
Jeder Zeiger auf Objekt des Typs
T1
können Zeiger auf Objekt eines anderen Typs T2
umgewandelt werden. Wenn T2
die Ausrichtung Anforderung ist nicht strenger als T1
die, Umwandlung der resultierenden Zeiger wieder in seinen ursprünglichen Typ Erträge der ursprüngliche Wert, da sonst die resultierende Zeiger kann nicht sicher dereferenziert werden. In jedem Fall kann die resultierende Zeiger nur sicher aufgelöst werden, wenn durch die Typ Aliasing Regeln (siehe unten) erlaubt Original:
Any pointer to object of type
T1
can be converted to pointer to object of another type T2
. If T2
's alignment requirement 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. In any case, the resulting pointer may only be dereferenced safely if allowed by the type aliasing rules (see below) The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
6)
Ein L-Wert Ausdruck des Typs
T1
umgewandelt in einen anderen Typ T2
verweisen. Das Ergebnis ist ein L-Wert oder xWert Bezugnahme auf dasselbe Objekt wie das Original Lvalue, aber mit einem anderen Typ. Keine temporäre erstellt wird, keine Kopie erstellt wird, werden keine Konstruktoren oder Umwandlung Funktionen aufgerufen. Der resultierende Sollwert kann nur sicher zugegriffen werden, wenn durch die Typ Aliasing Regeln (siehe unten) erlaubtOriginal:
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 (see below)The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
7)
Jeder Zeiger auf Funktion kann auf einen Zeiger auf eine andere Funktion umgewandelt werden. Der Aufruf der Funktion durch einen Zeiger auf eine andere Funktion Typ undefined, sondern Umwandlung solcher Zeiger wieder auf Zeiger auf die ursprüngliche Funktion type liefert den Zeiger auf die ursprüngliche Funktion .
Original:
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.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
8)
In einigen Implementierungen (insbesondere auf jedem POSIX-kompatiblen System) kann eine Funktion Zeiger auf ein Objekt Zeiger oder umgekehrt umgewandelt werden. Wenn die Implementierung unterstützt die Konvertierung in beide Richtungen, die Umstellung auf den ursprünglichen Typ liefert den ursprünglichen Wert, da sonst die resultierende Zeiger kann nicht dereferenziert oder aufgerufen werden sicher .
Original:
On some implementations (in particular, on any POSIX compatible system), 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.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
9)
Die Null-Zeiger-Wert von jeder Zeigertyp kann an jeden anderen Zeigertyp, was in der Null-Zeiger Wert dieses Typs umgewandelt werden. Beachten Sie, dass die Null-Pointer-Konstante nullptr oder irgendein anderer Wert vom Typ std::nullptr_t nicht auf einen Zeiger mit reinterpret_cast umgewandelt werden: implizite Konvertierung oder static_cast sollte für diesen Zweck verwendet werden .
Original:
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 with reinterpret_cast: implicit conversion or static_cast should be used for this purpose.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
10)
Ein rvalue Zeiger auf Elementfunktion kann Zeiger auf einen anderen Member-Funktion von einem anderen Typ umgewandelt werden. Die Umstellung auf den ursprünglichen Typ liefert den ursprünglichen Wert, da sonst die resultierende Zeiger kann nicht sicher verwendet werden .
Original:
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.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
11)
Ein rvalue Zeiger auf Member-Objekt von irgendeiner Klasse
T1
kann auf einen Zeiger auf ein anderes Mitglied Objekt einer anderen Klasse T2
umgewandelt werden. Wenn T2
die Ausrichtung ist nicht strenger als T1
die, die Umstellung auf den ursprünglichen Typ Erträge der ursprüngliche Wert, da sonst die resultierende Zeiger kann nicht sicher verwendet werden .Original:
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.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
Wie bei allen Besetzung Ausdrücke, ist das Ergebnis:
Original:
As with all cast expressions, the result is:
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
- ein L-Wert, wenn new_type ist ein L-Wert Referenz-Typ oder ein rvalue Bezug auf Art funktionieren;Original:an lvalue if new_type is an lvalue reference type or an rvalue reference to function type;The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions. - ein xWert wenn new_type ist ein rvalue Verweis auf Objekttyp;Original:an xvalue if new_type is an rvalue reference to object type;The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions. - a prvalue sonst .Original:a prvalue otherwise.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
[Bearbeiten]Keywords
[Bearbeiten]Geben Aliasing
Wenn ein Zeiger oder Referenz auf Objekt des Typs
T1
ist reintrepret_cast
(oder C-style cast) auf einen Zeiger oder Verweis auf eine andere Art T2
widersprechen, die Besetzung gelingt es immer wieder, aber die resultierende Zeiger oder eine Referenz kann nur zugegriffen werden, wenn einer der der folgenden Bedingungen erfüllt ist:Original:
When a pointer or reference to object of type
T1
is reintrepret_cast
(or C-style cast) to a pointer or reference to object of a different type T2
, the cast always succeeds, but the resulting pointer or reference may only be accessed if one of the following is true:The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
- T2 ist die (möglicherweise cv-qualifiziert) dynamische Typ des ObjektsOriginal:T2 is the (possibly cv-qualified) dynamic type of the objectThe text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
- T2 und T1 sind beide (möglicherweise mehrstufige, möglicherweise auf jeder Ebene cv-qualifiziert) Zeiger auf denselben Typ T3 (seit C++11)Original:T2 and T1 are both (possibly multi-level, possibly cv-qualified at each level) pointers to the same type T3 (seit C++11)The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
- T2 ist die (möglicherweise cv-qualifiziert) mit oder ohne Vorzeichen Variante des dynamischen Typ des ObjektsOriginal:T2 is the (possibly cv-qualified) signed or unsigned variant of the dynamic type of the objectThe text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
- T2 ist ein Aggregat Typ oder eine Vereinigung Typs, die eine der oben genannten Typen als Element oder nicht-statische Element (einschließlich, rekursiv Elemente subaggregates und nicht-statische Datenelemente der enthaltenen Gewerkschaften) gilt: Daraus sicher macht gegossen von dem ersten Teil einer Struktur und aus einem Element einer Vereinigung der struct / union, die es enthält .Original:T2 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): this makes it safe to cast from the first member of a struct and from an element of a union to the struct/union that contains it.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
- T2 ist eine (möglicherweise cv-qualifiziert) Basisklasse der dynamische Typ des ObjektsOriginal:T2 is a (possibly cv-qualified) base class of the dynamic type of the objectThe text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
- T2 is char or unsignedchar
Wenn T2 nicht erfüllen diese Anforderungen, den Zugriff auf das Objekt durch das neue Zeiger oder eine Referenz aufruft undefiniertem Verhalten. Dies wird als strenge Alias Regel bekannt und gilt für beide C + + und C Programmiersprachen .
Original:
If T2 does not satisfy these requirements, accessing the object through the new pointer or reference invokes undefined behavior. This is known as the strict aliasing rule and applies to both C++ and C programming languages.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
[Bearbeiten]Beispiel
Zeigt einige Verwendungen von reinterpret_cast:
Original:
Demonstrates some uses of reinterpret_cast:
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
#include <cstdint>#include <cassert>#include <iostream>int f(){return42;}int main(){int i =7; // pointer to integer and back uintptr_t v1 =reinterpret_cast<uintptr_t>(&i);// static_cast is an errorstd::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 backvoid(*fp1)()=reinterpret_cast<void(*)()>(f);// fp1(); undefined behaviorint(*fp2)()=reinterpret_cast<int(*)()>(fp1);std::cout<<std::dec<< fp2()<<'\n';// safe // type aliasing through pointerchar* p2 =reinterpret_cast<char*>(&i);if(p2[0]=='\x7')std::cout<<"This system is little-endian\n";elsestd::cout<<"This system is big-endian\n"; // type aliasing through referencereinterpret_cast<unsignedint&>(i)=42;std::cout<< i <<'\n';}
Output:
The value of &i is 0x7fff352c3580 42 This system is little-endian 42
[Bearbeiten]Siehe auch
const_cast Konvertierung | hinzugefügt oder entfernt const Original: adds or removes const The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. |
static_cast Konvertierung | führt grundlegende Umbauten Original: performs basic conversions The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. |
dynamic_cast Konvertierung | führt überprüft polymorphe Umwandlungen Original: performs checked polymorphic conversions The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. |