Conversión reinterpret_cast
You can help to correct and verify the translation. Click here for instructions.
Sintaxis
reinterpret_cast < new_type > ( expression )
|
|||||||||
new_type .new_type.You can help to correct and verify the translation. Click here for instructions.
Explicación
static_cast, pero como const_cast, la expresión reinterpret_cast no compila todas las instrucciones de la CPU. Es puramente una directiva del compilador que indica al compilador que trate la secuencia de bits (representación de objetos) de expression como si tuviera el tipo new_type .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.You can help to correct and verify the translation. Click here for instructions.
reintepret_cast, excepto cuando tales conversiones sería desechado constness''' o volatilidad .reintepret_cast, except when such conversions would cast away constness or volatility.You can help to correct and verify the translation. Click here for instructions.
expression. (desde C++11)expression. (desde C++11)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.
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.
You can help to correct and verify the translation. Click here for instructions.
nullptr se puede convertir en cualquier tipo integral, como si fuera (void*)0, pero ningún valor, ni siquiera nullptr se puede convertir en std::nullptr_t: static_cast debe utilizarse para ese propósito. (desde C++11)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. (desde C++11)You can help to correct and verify the translation. Click here for instructions.
T1 tipo se puede convertir a puntero a objeto de otro tipo T2. Si el requisito T2 de alineación no es más estricta que T1, la conversión del puntero resultante de nuevo a sus rendimientos de tipo de original del valor original, de lo contrario el puntero resultante no puede dejar de hacer referencia de forma segura. En cualquier caso, el puntero resultante sólo puede dejar de hacer referencia de manera segura si es permitido por las reglas de tipo' aliasing (ver más abajo) 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) You can help to correct and verify the translation. Click here for instructions.
T1 tipo se puede convertir para hacer referencia a otro tipo T2. El resultado es un valor-o refiriéndose al mismo objeto que el original lvalue, pero con un tipo diferente xValue. Sin temporal se crea ninguna copia se hace, ningún constructor o funciones de conversión son llamados. La referencia de resultado sólo se puede acceder con seguridad si es permitido por las reglas de tipo' aliasing (ver más abajo)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)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.
You can help to correct and verify the translation. Click here for instructions.
nullptr puntero nulo constante o cualquier otro valor de std::nullptr_t tipo no se puede convertir a un puntero con reinterpret_cast: conversión implícita o static_cast se debe utilizar para este propósito .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.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.
T1 clase puede convertirse en un puntero a otro objeto miembro de otra clase T2. Si la alineación T2 no es más estricta que T1, la conversión a los rendimientos de tipo de original del valor original, de lo contrario el puntero resultante no puede ser utilizado con seguridad .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.You can help to correct and verify the translation. Click here for instructions.
Como con todas las expresiones de conversión, el resultado es:
- Un l-valor si tipo-de-destino es un tipo referencia a l-valor o un tipo referencia r-valor a función (desde C++11);
|
(desde C++11) |
- Un pr-valor en caso contrario.
Palabras clave
Alias de tipos
Cuando se intenta leer o modificar el valor almacenado de un objeto de tipo DynamicType a través de un glvalue de tipo AliasedType, el comportamiento está indefinido a menos que uno de los siguientes sea verdadero:
AliasedTypeyDynamicTypeson similares.AliasedTypees la la variante (posiblemente calificada-cv) con signo o sin signo deDynamicType.AliasedTypees std::byte (desde C++17),char, ounsigned char: esto permite el examen de la representación de objeto de cualquier objeto como un array de bytes.
Informalmente, dos tipos son similares si, ignorando la calificación-cv de nivel superior:
- son del mismo tipo; o
- ambos son punteros, y los tipos apuntados son similares; o
- ambos son punteros a miembro de la misma clase, y los tipos de los miembros a los que se apunta son similares; o
|
(hasta C++20) |
|
(desde C++20) |
Por ejemplo:
const int * volatile *yint * * constson similares;const int (* volatile S::* const)[20]yint (* const S::* volatile)[20]son similares;int (* const *)(int *)yint (* volatile *)(int *)son similares;int (S::*)() constyint (S::*)()no son similares;int (*)(int *)yint (*)(const int *)no son similares;const int (*)(int *)yint (*)(int *)no son similares;int (*)(int * const)yint (*)(int *)son similares (son del mismo tipo);std::pair<int, int>ystd::pair<const int, int>no son similares.
Esta regla habilita el análisis de alias basado en tipo, en el que un compilador supone que el valor leído a través de un glvalue de un tipo no se modifica por una escritura a un glvalue de un tipo diferente (sujeto a las excepciones mencionadas anteriormente).
Observa que varios compiladores de C++ relajan esta regla como una extensión no estándar del lenguaje para permitir el acceso al tipo equivocado a través del miembro inactivo de una unión (tal acceso no está indefinido en C).
Ejemplo
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';
}
Salida:
The value of &i is 0x7fff352c3580
42
This system is little-endian
42
Ver también
| const_cast conversión | agrega o quita 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 conversión | realiza conversiones básicas
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 conversión | realiza conversiones marcadas polimórficos
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. |