reinterpret_cast conversion
来自cppreference.com
该页由英文版wiki使用Google Translate机器翻译而来。
该翻译可能存在错误或用词不当。鼠标停留在文本上可以看到原版本。你可以帮助我们修正错误或改进翻译。参见说明请点击这里. |
类型之间的转换,通过重新诠释底层的位模式.
原文:
Converts between types by reinterpreting the underlying bit pattern.
目录 |
[编辑] 语法
reinterpret_cast < new_type > ( expression )
|
|||||||||
返回值类型
new_type
[编辑] 解释
与static_cast,但像const_cast,reinterpret_cast表达不编译任何CPU指令。这纯粹是一个编译器指令指示编译器把位的序列对象表示expression,如果它的类型new_type.
原文:
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.
只有下列转换可以reintepret_cast,但这种转换时,将抛弃“常量性”或“波动”
原文:
Only the following conversions can be done with reintepret_cast, except when such conversions would cast away constness or volatility.
1)
积分,枚举,指针,或指针到成员类型的表达式可以被转换成其自己的类型。作为
expression
的值所得到的值是相同的。 (C++11 起)原文:
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
. (C++11 起)2)
任何指针可以转换为任何整数类型,大到足以容纳的指针值(例如std::uintptr_t)
原文:
Any pointer can be converted to any integral type large enough to hold the value of the pointer (e.g. to std::uintptr_t)
3)
任何整型或枚举类型值可以转换为指针类型。 ,否则由此引起的指针不能安全地被解除引用的指针是保证有足够的规模和返回相同的指针类型的整数转换为它的原始值。空指针常量NULL或整数零不能保证产生的目标类型的空指针值;static_cast或<div class="t-tr-text">隐式转换
应该用于这个目的.....原文:
implicit conversion
这段文字是通过 [http://translate.google.com Google Translate] 自动翻译生成的。
您可以帮助我们检查、纠正翻译中的错误。详情请点击[http://en.cppreference.com/w/Cppreference:MachineTranslations 这里]。
您可以帮助我们检查、纠正翻译中的错误。详情请点击[http://en.cppreference.com/w/Cppreference:MachineTranslations 这里]。
原文:
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
隐式转换</div> should be used for this purpose.
原文:
implicit conversion
这段文字是通过 [http://translate.google.com Google Translate] 自动翻译生成的。
您可以帮助我们检查、纠正翻译中的错误。详情请点击[http://en.cppreference.com/w/Cppreference:MachineTranslations 这里]。
您可以帮助我们检查、纠正翻译中的错误。详情请点击[http://en.cppreference.com/w/Cppreference:MachineTranslations 这里]。
4)
类型std::nullptr_t,包括nullptr的任何值可以被转换为任何整数类型,就好像它是(void*)0,但没有任何价值,甚至不nullptr可以转换为std::nullptr_t:static_cast应用于上述用途的。 (C++11 起)
原文:
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. (C++11 起)
5)
对象类型
T1
任何指针可以被转换为指针的另一类型T2
对象。如果T2
的对齐要求是不严格的比T1
,转换的结果指针回到它的原始类型产量的原始值,否则由此引起的指针不能被解除引用安全。在任何情况下,所得到的指针可能只被解除引用安全,如果所允许的“类型别名”规则(见下文) 原文:
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) 6)
类型
T1
一个lvalue表达式可以被转换为另一种类型T2
引用。其结果是一个lvalue或xvalue参照作为原始的左值,但用不同的类型相同的对象。任何临时被创建时,没有复制,没有构造函数或转换函数被调用。由此产生的参考只能访问安全允许的情况下“走样”规则(见下文)原文:
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)7)
任何函数指针可以被转换为一个指针到一个不同的函数类型。通过指针调用函数到不同的函数类型是不确定的,但这样的指针转换为指针,以原有的功能型产生的指针到原来的功能.
原文:
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.
8)
在一些实施方式中(尤其是,在任何POSIX兼容的系统)中,函数指针可以被转换为一个对象的指针或反之亦然。如果实现支持在两个方向上的转换,转换为原始类型产生的原始值,否则由此引起的指针不能被解除引用或称为安全.
原文:
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.
9)
任何指针类型的空指针值可以被转换为任何其他指针类型,在该类型的空指针值所导致。注意的空指针常量nullptr或任何其他值类型std::nullptr_t不能被转换成一个指针,reinterpret_cast:隐式转换或static_cast应该用于这个目的.....
原文:
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.
10)
右值的成员函数指针可以被转换为指针到不同的成员函数的不同类型。原始类型的转换产生的原始值,否则所得到的指针不能被安全地使用.
原文:
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.
11)
一个rvalue类
T1
的一些成员对象的指针可以被转换成一个指向另一个类的对象T2
的另一名成员。 T2
的对齐比T1
的原始值,转换为原始类型的收益率是不严格的,否则由此引起的指针不能安全使用的.原文:
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.与所有的强制转换表达式,其结果是:
原文:
As with all cast expressions, the result is:
- 如果一个左值new_type是一个左值引用类型或一个右值引用为函数类型原文:an lvalue if new_type is an lvalue reference type or an rvalue reference to function type;
- 值为xValue,如果new_type是一个右值引用的对象类型原文:an xvalue if new_type is an rvalue reference to object type;
- 一个prvalue否则.
[编辑] 关键字
[编辑] 类型别名
当指针或引用类型的对象
T1
reintrepret_cast
(或C风格的转换)的指针或引用对象的不同类型的T2
,演员总是成功的,但产生的指针或引用仅可访问之一以下是真实的原文:
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:- T2(CV合格)的动态类型的对象原文:T2 is the (possibly cv-qualified) dynamic type of the object
- T2和T1都(可能是多层次的,可能被cv资格在每个级别)指向相同类型T3(C++11 起)的指针原文:T2 and T1 are both (possibly multi-level, possibly cv-qualified at each level) pointers to the same type T3 (C++11 起)
- T2的动态类型的对象(可能是CV合格的)符号或无符号的变体原文:T2 is the (possibly cv-qualified) signed or unsigned variant of the dynamic type of the object
- T2是一个聚合类型或联合类型,其持有的上述类型的元素或者非静态成员(包括递归元素的子聚合和非静态数据成员所包含的工会),这使得它的安全投从第一的结构和元素的工会成员包含它的结构/联合.原文: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.
- T2是一个(可能是CV合格的)基类的对象的动态类型原文:T2 is a (possibly cv-qualified) base class of the dynamic type of the object
- T2 is char or unsigned char
如果T2不能满足这些要求,通过新的指针访问对象或引用调用未定义的行为。这就是所谓的“严格走样”规则和适用于用C + +和C编程语言.
原文:
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.
[编辑] 示例
演示的一些使用reinterpret_cast的
原文:
Demonstrates some uses of reinterpret_cast:
#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'; }
输出:
The value of &i is 0x7fff352c3580 42 This system is little-endian 42
[编辑] 另请参阅
const_cast会转换 | 添加或移除常量
|
的static_cast的转换 | 执行基本的转换
|
dynamic_cast的转换 | 执行检查的多态的转换
原文: performs checked polymorphic conversions |