Namespace
Varianti

reinterpret_cast conversion

Da cppreference.com.
< cpp‎ | language

 
 
Linguaggio C + +
Temi generali
Original:
General topics
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Controllo del flusso
Original:
Flow control
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Dichiarazioni esecuzione condizionale
Original:
Conditional execution statements
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Iterazione dichiarazioni
Original:
Iteration statements
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Vai dichiarazioni
Original:
Jump statements
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Funzioni
Original:
Functions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
dichiarazione di funzione
lambda funzione dichiarazione
funzione di modello
specificatore inline
eccezioni specifiche (deprecato)
noexcept specificatore (C++11)
Eccezioni
Original:
Exceptions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Spazi dei nomi
Original:
Namespaces
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Tipi
Original:
Types
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
decltype specifier (C++11)
Specifiers
Original:
Specifiers
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
cv specificatori
Durata di stoccaggio specificatori
constexpr specificatore (C++11)
specificatore auto (C++11)
alignas specificatore (C++11)
Inizializzazione
Original:
Initialization
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Letterali
Original:
Literals
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Espressioni
Original:
Expressions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
rappresentazioni alternative
Utilities
Original:
Utilities
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Tipi
Original:
Types
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
typedef declaration
Tipo alias dichiarazione (C++11)
attributi (C++11)
Lancia
Original:
Casts
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
conversioni implicite
const_cast conversion
static_cast conversion
dynamic_cast conversion
reinterpret_cast conversion
Fusione C-stile e funzionale
Occupazione della memoria
Original:
Memory allocation
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Classi
Original:
Classes
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Specifiche per una classe di funzioni proprietà
Original:
Class-specific function properties
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
esplicito (C++11)
statico
Funzioni membro speciali
Original:
Special member functions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Modelli
Original:
Templates
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
classe template
funzione di modello
modello di specializzazione
parametri confezioni (C++11)
Varie
Original:
Miscellaneous
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Montaggio in linea
 
Converte tra i tipi reinterpretando il modello sottostante po '.
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.

Indice

[modifica] Sintassi

reinterpret_cast < new_type > ( expression )
Restituisce un valore di tipo 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.

[modifica] Spiegazione

A differenza di static_cast, ma come const_cast, l'espressione reinterpret_cast non compilare in tutte le istruzioni della CPU. E 'puramente una direttiva del compilatore, che indica al compilatore di trattare la sequenza di bit (rappresentazione dell'oggetto) di expression come se avesse il tipo 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.
Solo le seguenti conversioni può essere fatto con reintepret_cast, tranne quando tali conversioni avrebbe gettato via constness''' o 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)
Un'espressione di integrale, enumerazione, puntatore, o puntatore-a-membro del tipo può essere convertito in proprio tipo. Il valore risultante è uguale al valore di expression. (dal 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. (dal 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)
Qualsiasi puntatore può essere convertito in qualsiasi tipo integrale sufficiente a contenere il valore del puntatore (ad es 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)
Un valore di qualsiasi tipo integrale o enumerazione può essere convertito in un tipo di puntatore. Un puntatore convertito in un numero intero di dimensioni sufficienti e torna al tipo di puntatore stesso è garantito per avere il suo valore originale, altrimenti il ​​puntatore risultante non può essere dereferenziati sicuro. Il NULL puntatore nullo costante o zero integer non è garantito per produrre il valore di puntatore nullo del tipo di destinazione; static_cast o <div class="t-tr-text">conversione implicita
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.
deve essere utilizzato per questo scopo.
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)
Qualsiasi valore di std::nullptr_t tipo, compresi nullptr può essere convertito in qualsiasi tipo integrale, come se fosse (void*)0, ma nessun valore, nemmeno nullptr può essere convertito std::nullptr_t: static_cast deve essere utilizzato per questo scopo. (dal 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. (dal 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)
Qualsiasi puntatore a oggetto di T1 tipo può essere convertito a puntatore a oggetto di un altro tipo T2. Se l'allineamento T2 requisito non è più rigoroso di T1, la conversione del puntatore conseguente alle sue rese tipi di originale il valore originale, in caso contrario il puntatore risultante non può essere dereferenziato in modo sicuro. In ogni caso, il puntatore risultante può essere dereferenced sicuro se consentito dalle norme di aliasing di tipo' (vedi sotto)
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)
Un'espressione lvalue di T1 tipo può essere convertito per fare riferimento a un altro tipo T2. Il risultato è un lvalue o riferimento all'oggetto stesso dell'originale lvalue, ma con un diverso tipo xValue. Nessun temporaneo viene creato, nessuna copia è fatta, non costruttori o funzioni di conversione sono chiamati. Il riferimento risultante è accessibile solo in modo sicuro se consentito dalle norme di aliasing di tipo' (vedi sotto)
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)
Qualsiasi puntatore a funzione può essere convertito in un puntatore a una funzione di tipo diverso. Chiamare la funzione tramite un puntatore a un tipo di funzione diverso è indefinito, ma la conversione puntatore tali messaggi ai puntatore al tipo di funzione originale cede il puntatore alla funzione originale.
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)
In alcune implementazioni (in particolare, su qualsiasi sistema compatibile POSIX), un puntatore a funzione può essere convertito in un puntatore all'oggetto o viceversa. Se l'applicazione supporta la conversione in entrambe le direzioni, la conversione al tipo originale fornisce il valore originale, in caso contrario il puntatore risultante non può essere chiamato in modo sicuro o dereferenziato.
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)
Il valore del puntatore nullo qualsiasi tipo di puntatore può essere convertito in qualsiasi altro tipo di puntatore, causando il valore del puntatore nullo di quel tipo. Notare che il nullptr puntatore nullo costante o qualsiasi altro valore di std::nullptr_t tipo non può essere convertito in un puntatore con reinterpret_cast: conversione implicita o static_cast deve essere utilizzato per questo scopo.
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 puntatore alla funzione membro rvalue può essere convertito in un puntatore a funzione membro diverso di tipo diverso. Conversione al tipo originale produce il valore originale, altrimenti il ​​puntatore risultante non può essere utilizzato in modo sicuro.
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 puntatore rvalue di opporsi membro di qualche T1 classe può essere convertito in un puntatore a un altro oggetto membro di un altro T2 classe. Se l'allineamento T2 non è più rigoroso T1, la conversione delle rese tipo originali il valore originale, altrimenti il ​​puntatore risultante non può essere utilizzato in modo sicuro.
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.
Come con tutti espressioni cast, il risultato è:
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.
  • un lvalue se new_type è un tipo di riferimento o un riferimento lvalue rvalue un funzionamento tipo;
    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 se new_type è un riferimento rvalue per tipo di oggetto;
    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 altrimenti.
    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.

[modifica] Parole chiave

reinterpret_cast

[modifica] Digitare aliasing

Quando un puntatore o un riferimento a un oggetto di tipo T1 è reintrepret_cast (o C-style cast) a un puntatore o riferimento a oggetto di un T2 tipo diverso, il cast riesce sempre, ma il puntatore risultante o di riferimento può accedere solo se uno dei delle seguenti condizioni:
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 è il tipo (possibilmente cv-qualificata) dinamica dell'oggetto
    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 e T1 sono entrambi (possibilmente multi-livello, possibilmente cv-qualificato a ciascun livello) puntatori al (dal C++11) stesso tipo T3
    Original:
    T2 and T1 are both (possibly multi-level, possibly cv-qualified at each level) pointers to the same type T3 (dal 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 è la variante (forse cv-qualificata) con o senza segno di tipo dinamico dell'oggetto
    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 è un tipo di aggregazione o di un tipo di unione che contiene uno dei tipi di cui sopra come membro elemento o non statici (tra cui, in modo ricorsivo, elementi di aggregazioni secondarie e non-membri dati statici dei sindacati contenuti): questo lo rende sicuro per lanciare dal primo membro di una struttura e da un elemento di unione per l'unione struct / che contiene.
    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 è un (forse cv-qualificata) classe di base di tipo dinamico dell'oggetto
    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 unsigned char
Se T2 non soddisfa questi requisiti, l'accesso all'oggetto attraverso il nuovo puntatore o un riferimento richiama un comportamento indefinito. Questo è noto come il rigoroso aliasing regola e si applica sia C + + e linguaggi di programmazione 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.

[modifica] Esempio

Viene illustrato alcuni utilizzi di 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() { 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

[modifica] Vedi anche

const_cast conversione
aggiunge o rimuove 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.
[modifica]
static_cast conversione
esegue conversioni di 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.
[modifica]
dynamic_cast conversione
esegue conversioni controllato polimorfici
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.
[modifica]