Espacios de nombres
Variantes
Acciones

reinterpret_cast conversion

De cppreference.com
< cpp‎ | language

 
 
Lenguaje C++
Los temas generales
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.
Control de flujo
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.
Sentencias condicionales ejecución
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.
Instrucciones de iteración
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.
Ir declaraciones
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.
Funciones
Original:
Functions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
declaración de la función
lambda declaración de la función
función de plantilla
especificador inline
especificaciones de excepción (obsoleto)
noexcept especificador (C++11)
Excepciones
Original:
Exceptions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Los espacios de nombres
Original:
Namespaces
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Tipos
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)
Los especificadores
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 especificadores
almacenamiento duración especificadores
constexpr especificador (C++11)
auto especificador (C++11)
alignas especificador (C++11)
Inicialización
Original:
Initialization
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Los literales
Original:
Literals
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Expresiones
Original:
Expressions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
representaciones alternativas
Utilidades
Original:
Utilities
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Tipos
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
alias de tipo de declaración (C++11)
atributos (C++11)
Lanza
Original:
Casts
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
conversiones implícitas
const_cast conversion
static_cast conversion
dynamic_cast conversion
reinterpret_cast conversion
C-cast estilo y funcional
De asignación de 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.
Clases
Original:
Classes
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Clase propiedades específicas de la función
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.
Funciones especiales miembro
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.
Plantillas
Original:
Templates
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
clase de plantilla
función de plantilla
plantilla de especialización
paquetes de parámetros (C++11)
Varios
Original:
Miscellaneous
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Montaje en línea
 
Convierte entre distintos tipos de reinterpretar el patrón de bits subyacente .
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.

Contenido

[editar] Sintaxis

reinterpret_cast < new_type > ( expression )
Devuelve un valor de 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.

[editar] Explicación

A diferencia de 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 .
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.
Sólo las siguientes conversiones se puede hacer con reintepret_cast, excepto cuando tales conversiones sería desechado constness''' o volatilidad .
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)
Una expresión de enumeración integral, puntero, o puntero a miembro tipo se puede convertir a su propio tipo. El valor resultante es el mismo que el valor de expression. (desde 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. (desde 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)
Cualquier puntero se puede convertir en cualquier tipo integral lo suficientemente grande como para mantener el valor del puntero (por ejemplo, a 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 valor de cualquier tipo integral o de enumeración se puede convertir en un tipo de puntero. Un puntero convertir en un entero de tamaño suficiente y de nuevo al tipo de puntero mismo está garantizado que tenga su valor original, de lo contrario el puntero resultante no puede dejar de hacer referencia de forma segura. El NULL puntero nulo constante o cero entero no está garantizado para dar el valor de puntero nulo del tipo de destino; static_cast o <div class="t-tr-text">conversión implícita
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.
se debe utilizar para este propósito .
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)
Cualquier valor de std::nullptr_t tipo, incluyendo 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)
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. (desde 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)
Cualquier puntero al objeto de 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)
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)
Una expresión lvalue de 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)
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)
Cualquier puntero a la función se puede convertir en un puntero a un tipo de función diferente. Llamar a la función a través de un puntero a un tipo diferente función no está definida, pero la conversión de puntero de nuevo a tal indicador al tipo de función original se obtiene el puntero a la función original .
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)
En algunas implementaciones (en particular, en cualquier sistema POSIX compatible), un puntero de función se pueden convertir a un puntero de objeto o viceversa. Si la aplicación admite la conversión en ambas direcciones, la conversión al tipo original se obtiene el valor original, de lo contrario el puntero resultante no puede dejar de hacer referencia o llamados de forma segura .
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)
El valor de puntero nulo de cualquier tipo de puntero se puede convertir en cualquier otro tipo de puntero, lo que resulta en el valor de puntero nulo de ese tipo. Tenga en cuenta que la 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 .
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 puntero a la función miembro rvalue se puede convertir a puntero a una función miembro diferente de un tipo diferente. Conversión al tipo original produce el valor original, de lo contrario el puntero resultante no puede ser utilizado con seguridad .
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 puntero a objeto rvalue miembro de alguna 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 .
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.
Como con todas las expresiones de conversión, el resultado es:
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 valor-si new_type es un tipo de referencia o una referencia de valor-valor-poder funcionar en su 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 si new_type es una referencia a rvalue tipo de objeto;
    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 de otra manera .
    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.

[editar] Palabras clave

reinterpret_cast

[editar] Escriba aliasing

Cuando un puntero o una referencia al objeto de tipo T1 es reintrepret_cast (o C-estilo cast) a un puntero o una referencia al objeto de T2 un tipo diferente, el elenco siempre tiene éxito, pero el puntero o referencia resultante sólo puede acceder a ella si uno de se cumple lo siguiente:
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 es el tipo (posiblemente cv-cualificada) dinámica del objeto
    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 y T1 son ambos (posiblemente multi-nivel, posiblemente cv-calificado en cada nivel) punteros a la (desde C++11) tipo T3 mismo
    Original:
    T2 and T1 are both (possibly multi-level, possibly cv-qualified at each level) pointers to the same type T3 (desde 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 es la variante (posiblemente cv-calificados) con o sin signo de tipo dinámico del objeto
    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 es un tipo agregado o un tipo de unión que tiene uno de los tipos antes mencionados como un elemento o miembro no estática (incluyendo, de forma recursiva, elementos de subagregados y los miembros no estáticos de datos de los sindicatos contenidos): esto hace que sea seguro para lanzar desde el primer miembro de una estructura y de un elemento de una unión para la unión struct / que 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 es una clase base (posiblemente cv-calificada) del tipo dinámico del objeto
    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
Si T2 no cumple con estos requisitos, el acceso al objeto a través del nuevo puntero o referencia invoca un comportamiento indefinido. Esto se conoce como el alias estricto' regla y se aplica tanto a C + + y lenguajes de programación 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.

[editar] Ejemplo

Muestra algunos usos 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() { 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

[editar] 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.
[edit]
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.
[edit]
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.
[edit]