意图执行低级转型实际动作可能取决于编译器。
reinterpret_cast<new_type>(expression);这是一个完全的编译器行为,它将expression的二进制序列解释成new_type。正因为他是从二进制的角度做转型,所以他可以“随便搞”,但也比较危险。
1) 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. (since C++11)
同类型转换将保持同样的值:
#include <iostream> using namespace std; int main() { int a=10; int b=reinterpret_cast<int>(a); cout<<b<<endl; return 0; }2) 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) 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 round-trip conversion in the opposite direction is not guaranteed; the same pointer may have multiple integer representations) The null pointer constant NULL or integer zero is not guaranteed to yield the null pointer value of the target type; static_cast or implicit conversion should be used for this purpose.
值类型也可以转成指针类型,并且能够保证指针类型指向的地址将有原来的值,不过这种转型是不安全的。
下面是2、3两条的例子
int main() { int a = 10; int *pa = &a; unsigned long b = reinterpret_cast<unsigned long>(pa); cout <<std::hex<< b << endl; cout <<std::hex<< pa << endl; int *pb = reinterpret_cast<int *>(b); cout << std::dec<<*pb << endl; system("pause"); return 0; }4) Any value of type std::nullptr_t, including nullptr can be converted to any integral type as if it were (void)0, but no value, not even nullptr can be converted to std::nullptr_t: static_cast should be used for that purpose. (since C++11)*
nullptr可以被转型成任意整型类型,但不能转型成nullptr_t。
int main() { int a = reinterpret_cast<int>(nullptr); std::nullptr_t p = reinterpret_cast<std::nullptr_t>(nullptr);//error std::nullptr_t p1 = static_cast<std::nullptr_t>(nullptr);//ok cout << a << endl;//输出0 system("pause"); return 0; }5) Any pointer to object of type T1 can be converted to pointer to object of another type cv T2. This is exactly equivalent to static_cast
static_cast<cv T2*>(static_cast<cv void*>(expression)) / struct B {}; struct C {}; int main() { B *pb = new B; C*pc = reinterpret_cast<C*>(pb);//ok C*pc1 = static_cast<C*>(pb);//error C*pc2 = static_cast<C*>(static_cast<void*>(pb));//ok system("pause"); return 0; }6) 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)
左值表达式可以转成另一个类型的引用类型。
int main() { int a = 65; //char &b = static_cast<char&>(a);//error char &b1 = reinterpret_cast<char&>(a);//ok cout << b1 << endl;//'A' system("pause"); return 0; }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) On some implementations (in particular, on any POSIX compatible system as required by dlsym), a function pointer can be converted to void or any other 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.*
函数指针可以转成void*再转回来。
double func1() { return 3.14; } int func2() { return 1; } typedef double(*pFunc1)(); typedef int(*pFunc2)(); int main() { pFunc1 p1 = func1; pFunc2 p2 = reinterpret_cast<pFunc2>(p1); void * v = reinterpret_cast<void *>(p1); pFunc1 p3 = reinterpret_cast<pFunc1>(v); cout << p3() << endl; cout << p2() << endl; system("pause"); return 0; }9) 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) 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.
类成员函数指针和对象指针也可以使用reinterpret_cast…
reinterpret_cast很强大,强大到可以“随便”转型,因为他是编译器面向二进制的转型,但安全性需要考虑。
cppreference
