Espaces de noms
Variantes
Actions

reinterpret_cast conversion

De cppreference.com
< cpp‎ | language

 
 
Langage C++
Sujets généraux
Contrôle de flux
Instructions conditionnelles
Instructions d'itération
Instructions de saut
Fonctions
déclaration de fonction
expression lambda
fonction générique
spécificateur inline
spécification d'exception (obsolète)
spécificateur noexcept (C++11)
Exceptions
Espaces de noms
Types
spécificateur decltype (C++11)
Qualificatifs
qualificatifs const et volatile
qualificatifs de stockage
qualificatif constexpr (C++11)
qualificatif auto (C++11)
qualificatif alignas (C++11)
Initialisation
Littéraux
Expressions
opérateurs alternatifs
Utilitaires
Types
déclaration typedef
déclaration d'alias de type (C++11)
attributs (C++11)
Jette
Original:
Casts
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
conversions implicites
conversion const_cast
conversion static_cast
conversion dynamic_cast
conversion reinterpret_cast
conversions style C et style fonction
Allocation de mémoire
Classes
Qualificatifs spécifiques aux membres de classe
Fonctions membres spéciales
Modèles
classes génériques
fonctions génériques
spécialisation de modèles
paquets de paramètres (C++11)
Divers
Assembleur
 
Convertit entre différents types en réinterprétant le motif binaire sous-jacente .
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.

Sommaire

[modifier]Syntaxe

reinterpret_cast <new_type> (expression)
Renvoie une valeur de type .. 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.

[modifier]Explication

Contrairement static_cast, mais comme const_cast, l'expression reinterpret_cast ne compile pas à toutes les instructions du processeur. C'est purement une directive de compilation qui indique au compilateur de traiter la séquence de bits (représentation de l'objet) de expression comme si elle avait le type new_type .
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.
Seules les conversions suivantes peut être fait avec reintepret_cast, sauf lorsque de telles conversions jetterait sans constante''' ou la volatilité .
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.
1)
L'expression d'une intégrale, dénombrement, pointeur ou pointeur à élément de type peut être transformé en son propre type. La valeur obtenue est la même que la valeur de expression. (depuis 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. (depuis C++11)
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
2)
N'importe quel pointeur peut être converti en un type intégral suffisamment grand pour contenir la valeur du pointeur (par exemple à std::uintptr_t)
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.
3)
Une valeur de tout type intégral ou énumération peut être converti en un type pointeur. Un pointeur converti en un nombre entier de taille suffisante et de retour à la nature même pointeur est assuré d'avoir sa valeur d'origine, sinon le pointeur résultant ne peut pas être déréférencé en toute sécurité. Le NULL pointeur NULL constante ou nulle entier n'est pas garanti pour obtenir la valeur de pointeur nul du type cible; static_cast ou <div class="t-tr-text">conversion implicite
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.
doit être utilisé à cet effet .
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
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
4)
Toute valeur de std::nullptr_t type, y compris nullptr peuvent être convertis en n'importe quel type intégral comme s'il s'agissait d'(void*)0, mais aucune valeur, pas même nullptr peut être converti en std::nullptr_t: static_cast doit être utilisé à cette fin. (depuis 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. (depuis C++11)
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
5)
Tout pointeur vers l'objet de T1 type peut être converti en pointeur vers l'objet d'un autre type de T2. Si exigence d'alignement T2 n'est pas plus strictes que T1, la conversion du pointeur résultant revenir à ses rendements de type d'origine de la valeur d'origine, sinon le pointeur résultant ne peut pas être déréférencé en toute sécurité. Dans tous les cas, le pointeur résultant ne peut être déréférencé en toute sécurité si cela est autorisé par les règles de type' aliasing (voir ci-dessous)
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.
6)
Une expression de lvalue T1 type peut être converti en une référence à un autre type de T2. Le résultat est une lvalue ou se référant à l'objet même que l'original lvalue, mais avec un autre type xValue. Aucune temporaire est créé, aucune copie n'est faite, aucun constructeur ou des fonctions de conversion sont appelés. La référence en résulte ne peut être consulté en toute sécurité si cela est autorisé par les règles de type' aliasing (voir ci-dessous)
Original:
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.
7)
Aucun pointeur de fonction peut être converti en un pointeur vers un type de fonction différente. Appel de la fonction à travers un pointeur vers un type de fonction différente est définie, mais la conversion pointeur vers une telle pointeur vers le type de fonction d'origine donne le pointeur vers la fonction d'origine .
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.
8)
Dans certaines implémentations (en particulier, sur n'importe quel système compatible POSIX), un pointeur de fonction peut être converti en un pointeur d'objet ou vice versa. Si la mise en œuvre soutient la conversion dans les deux sens, la conversion au type d'origine donne la valeur d'origine, sinon le pointeur résultant ne peut pas être déréférencé ou appelé en toute sécurité .
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.
9)
La valeur de pointeur null pointeur de tout type peut être converti en un autre type de pointeur, d'où la valeur du pointeur null de ce type. Notez que le pointeur NULL nullptr constante ou toute autre valeur de std::nullptr_t type ne peut pas être converti en un pointeur avec reinterpret_cast: conversion implicite ou static_cast doit être utilisé à cet effet .
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.
10)
Un pointeur vers une fonction membre rvalue peut être converti en pointeur vers une fonction membre différent d'un type différent. Conversion du type originel donne la valeur d'origine, sinon le pointeur résultant ne peut pas être utilisé en toute sécurité .
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.
11)
Un pointeur rvalue à l'objet membre d'une classe T1 peut être converti en un pointeur vers un autre objet membre d'une autre classe T2. Si l'alignement T2 n'est pas plus strictes que T1, la conversion des rendements de type d'origine de la valeur d'origine, sinon le pointeur résultant ne peut pas être utilisé en toute sécurité .
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.
Comme pour toutes les expressions de transtypage, le résultat est le suivant:
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.
  • une lvalue si new_type est un type de référence lvalue ou une référence rvalue à un type de fonction;
    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.
  • un xValue si new_type est une référence rvalue de type d'objet;
    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.
  • un prvalue autrement .
    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.

[modifier]Mots-clés

reinterpret_cast

[modifier]Tapez aliasing

Quand un pointeur ou une référence à l'objet de type T1 est reintrepret_cast (ou C-style cast) à un pointeur ou une référence à un objet T2 type différent, l'attribution réussit toujours, mais le pointeur ou de référence résultant ne peut être accessible que si l'un des ce qui suit est vrai:
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.
  • T2 est le type (peut-cv-qualifié) dynamique de l'objet
    Original:
    T2 is the (possibly cv-qualified) dynamic type of the object
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • T2 et T1 sont tous les deux (éventuellement multi-niveau, éventuellement cv-qualifié à chaque niveau) des pointeurs vers le même type (depuis C++11) T3
    Original:
    T2 and T1 are both (possibly multi-level, possibly cv-qualified at each level) pointers to the same type T3 (depuis 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 est la variante (éventuellement cv-qualifié) signé ou non signé du type dynamique de l'objet
    Original:
    T2 is the (possibly cv-qualified) signed or unsigned variant of the dynamic type of the object
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • T2 est un type d'agrégat ou un type d'union qui détient l'un des types mentionnés ci-dessus en tant que membre ou élément non-statique (y compris, de manière récursive, des éléments de sous-agrégats et non les données membres statiques des syndicats contenus): il est donc sûr de jeter du premier élément d'une structure et d'un élément d'un syndicat de l'union struct / qu'il contient .
    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 est une classe de base (éventuellement qualifié cv) du type dynamique de l'objet
    Original:
    T2 is a (possibly cv-qualified) base class of the dynamic type of the object
    The 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
Si T2 ne satisfait pas à ces exigences, l'accès à l'objet à travers le nouveau pointeur ou de référence invoque un comportement indéfini. Ceci est connu comme le' aliasing règle stricte et s'applique à la fois C + + et les langages de programmation C .
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.

[modifier]Exemple

Montre certaines utilisations de 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.

#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';}

Résultat :

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

[modifier]Voir aussi

conversion const_cast
ajoute ou supprime 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.
[edit]
conversion static_cast
effectue des conversions de base
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.
[edit]
conversion dynamic_cast
effectue vérifiés conversions polymorphes
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.
[edit]
close