reinterpret_cast conversion
Da 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. |
Converte entre os tipos, reinterpretando o padrão de bits subjacente.
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.
Índice |
[editar] Sintaxe
reinterpret_cast < new_type > ( expression )
|
|||||||||
Retorna um valor de
new_type
tipo.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.
[editar] Explicação
Ao contrário static_cast, mas como const_cast, a expressão reinterpret_cast não compila a quaisquer instruções da CPU. É puramente uma diretiva de compilação que instrui o compilador a tratar a seqüência de bits (representação do objeto) de expression como se tivesse o 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.
You can help to correct and verify the translation. Click here for instructions.
Apenas as seguintes conversões podem ser feitas com reintepret_cast, excepto quando essas conversões seria lançado fora constness''' ou volatilidade.
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)
Uma expressão de enumeração, integral, ponteiro, ou ponteiro-para-membro tipo pode ser convertido para o seu próprio tipo. O valor resultante é o mesmo que o 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.
You can help to correct and verify the translation. Click here for instructions.
2)
Qualquer ponteiro pode ser convertido para qualquer tipo integral suficientemente grande para conter o valor do apontador (por exemplo, para 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.
You can help to correct and verify the translation. Click here for instructions.
3)
Um valor de qualquer tipo integral ou enumeração pode ser convertido para um tipo de ponteiro. Um ponteiro convertido para um número inteiro de tamanho suficiente e de volta para o mesmo tipo de ponteiro é garantido para ter o seu valor original, caso contrário, o ponteiro resultante não pode ser dereferenced com segurança. O NULL ponteiro nulo constante ou inteiro igual a zero não é garantido para produzir o valor de ponteiro nulo do tipo de destino; static_cast <div class="t-tr-text">conversão implícita
ou deve ser utilizada para este fim.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
conversão implícita</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)
Qualquer valor de std::nullptr_t tipo, incluindo nullptr pode ser convertido para qualquer tipo integral, como se fosse (void*)0, mas nenhum valor, nem mesmo nullptr pode ser convertido para std::nullptr_t: static_cast deve ser usada para esse fim. (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.
You can help to correct and verify the translation. Click here for instructions.
5)
Qualquer ponteiro de objeto de
T1
tipo pode ser convertido para o ponteiro de objeto de outro tipo T2
. Se requisito T2
do alinhamento não é mais rigoroso do que T1
, a conversão do ponteiro resultante de volta para os seus rendimentos de tipo originais do valor original, caso contrário, o ponteiro resultante não pode ser dereferenced com segurança. Em qualquer caso, o ponteiro resultante só pode ser dereferenced com segurança se permitido pelas regras do tipo' aliasing (ver abaixo) 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)
Uma expressão lvalue de
T1
tipo pode ser convertida para fazer referência a um outro tipo T2
. O resultado é um lvalue ou xValue referindo-se ao mesmo objecto que o lvalue original, mas com um tipo diferente. Não temporário é criado, nenhuma cópia é feita, nenhum construtor ou funções de conversão são chamados. A referência resultante só pode ser acessado com segurança se permitido pelas regras do tipo aliasing (veja abaixo)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.
You can help to correct and verify the translation. Click here for instructions.
7)
Qualquer ponteiro de função pode ser convertido para um apontador para um tipo de função diferente. Chamando a função através de um ponteiro para um tipo de função diferente é indefinido, mas a conversão de tais ponteiro volta para ponteiro para o tipo de função original produz o ponteiro para a função 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.
You can help to correct and verify the translation. Click here for instructions.
8)
Em algumas implementações (em particular, em qualquer sistema POSIX compatível), um ponteiro de função pode ser convertido para um ponteiro de objecto ou vice-versa. Se a aplicação suporta a conversão em ambos os sentidos, a conversão para o tipo original produz o valor original, caso contrário o ponteiro resultante não pode ser dereferenced ou chamado com segurança.
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)
O valor de ponteiro nulo de qualquer tipo de ponteiro pode ser convertido em qualquer outro tipo de ponteiro, o que resulta no valor de ponteiro nulo desse tipo. Note-se que a constante nullptr ponteiro nulo ou qualquer outro valor de std::nullptr_t tipo não pode ser convertido para um ponteiro com reinterpret_cast: conversão implícita ou static_cast deve ser usada para esse fim.
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)
Um ponteiro de rvalue para função de membro pode ser convertido para um ponteiro para função de membro diferente de um tipo diferente. A conversão para o tipo original produz o valor original, caso contrário, o ponteiro resultante não pode ser usado com segurança.
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)
Um ponteiro rvalue ao objeto membro de alguma
T1
classe pode ser convertido para um ponteiro a outro objeto membro de outra T2
classe. Se o alinhamento T2
não é mais rigoroso do que T1
, a conversão para o rendimento do tipo de origem o valor original, caso contrário, o ponteiro resultante não pode ser usado com segurança.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.
Tal como acontece com todas as expressões fundido, o resultado é:
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.
- um lvalue se new_type é um tipo de referência ou uma referência lvalue rvalue a funcionar 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. - um xValue se new_type é uma referência para 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. - uma outra forma prvalue.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] Palavras-chave
[editar] Digite aliasing
Quando um ponteiro ou referência a objeto do tipo
T1
é reintrepret_cast
(ou estilo C elenco) para um ponteiro ou referência ao objeto de um tipo diferente T2
, o elenco sempre bem-sucedida, mas o ponteiro resultante ou de referência pode ser acessado somente se uma das o seguinte é verdadeiro: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 é o tipo (possivelmente cv qualificado) dinâmica do objetoOriginal: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 e T1 são tanto (possivelmente multi-nível, possivelmente cv-qualificados em cada nível) ponteiros para o (desde C++11) mesmo tipo T3Original: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 é a variante (possivelmente cv qualificado) assinado ou não assinado do tipo dinâmico do objetoOriginal: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 é um tipo de agregado ou um tipo de união que detém um dos tipos mencionados como membro elemento ou não-estático (incluindo, recursivamente, elementos de subaggregates e membros não-estáticos de dados dos sindicatos constantes): isso torna seguro para lançar a partir do primeiro membro de uma estrutura e de um elemento de uma união com a união struct / que o contém.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 é uma classe base (possivelmente cv-qualificado) do tipo dinâmico do objetoOriginal: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 unsigned char
Se T2 não satisfaz estes requisitos, acessando o objeto através do novo ponteiro ou referência invoca um comportamento indefinido. Isto é conhecido como a regra estrita aliasing' e aplica-se a ambos os C + + e linguagens de programação 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.
You can help to correct and verify the translation. Click here for instructions.
[editar] Exemplo
Demonstra algumas utilizações 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.
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'; }
Saída:
The value of &i is 0x7fff352c3580 42 This system is little-endian 42
[editar] Veja também
conversão const_cast | adiciona ou remove 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. |
conversão static_cast | executa as conversões 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. |
conversão dynamic_cast | executa verificados conversões polimórficas
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. |