reinterpret_cast在大多数情况下是没有用的吗? [英] Is reinterpret_cast mostly useless?
问题描述
我以前已经阅读过有关使用reinterpret_cast
的各种问题,而且我还阅读了C ++标准中的相关措辞.从本质上讲,归结为指针到指针reinterpret_cast
操作的结果不能安全地用于任何其他操作,除非将其强制转换回原始指针类型.>
但是,在实践中,reinterpret_cast
的大多数实际用法似乎是基于(错误)的假设,即reinterpret_cast
与C样式强制转换相同.例如,我已经看到很多代码,出于字符集转换例程的目的,这些代码使用reinterpret_cast
从char*
强制转换为unsigned char*
.这是完全无害的,但严格来说,它不是可移植的-无法保证当尝试取消引用unsigned char*
指针时,从char*
到unsigned char*
的reinterpret_cast
不会使程序崩溃.
根据标准,
但是在许多情况下,我们会想要(并且应该能够)在不同的指针类型之间安全地进行转换.例如:uint16_t*
到新的C ++ 0x char16_t*
,或者实际上是指向基本数据类型的任何指针,该指针的大小/对齐方式与原始类型相同.但是reinterpret_cast
不提供任何保证.
问题:我们如何安全地在指向相同大小/对齐方式的基本数据类型的指针之间进行转换,例如char*
-> unsigned char*
?由于reinterpret_cast
似乎不能保证这确实有效,因此C样式强制转换是这里唯一安全的选择吗?
当尝试取消引用未签名的char *指针时,无法保证从char *到unsigned char *的reinterpret_cast不会使程序崩溃.
您无法以其他任何方式进行此类转换,因此必须不得不信任编译器使用此完全合理的转换所做的工作.
由于reinterpret_cast似乎不能保证此方法确实有效,因此C样式强制转换是这里唯一安全的选择吗?
C样式强制转换将仅映射到reinterpret_cast
,因此它将完全相同.在某些时候,您必须信任您的编译器.该标准有一个限制,它只能说不.请阅读您的编译器手册".当涉及到交叉转换指针时,这就是这一点.它允许您使用unsigned char
左值读取char
.不能将char*
强制转换为可用的unsigned char*
的编译器几乎无法使用,并且由于该原因不存在.
I've read various previous questions about the use of reinterpret_cast
, and I've also read the relevant wording in the C++ standard. Essentially, what it comes down to is that the result of a pointer-to-pointer reinterpret_cast
operation can't safely be used for anything other than being cast back to the original pointer type.
In practice, however, most real-world uses of reinterpret_cast
seem to be based on the (wrong) assumption that a reinterpret_cast
is the same as a C-style cast. For example, I've seen lots of code which uses reinterpret_cast
to cast from char*
to unsigned char*
for the purpose of character set conversion routines. This is completely harmless, yet strictly speaking it's not portable - there's no guarantee that a reinterpret_cast
from char*
to unsigned char*
won't crash your program when you try to dereference the unsigned char*
pointer.
It's seems the only other real use of reinterpret_cast
that has any real guarantees, according to the standard, is converting from pointer to integer, and vice-versa.
And yet there are many cases where we'd want (and should be able to) safely convert between different pointer types. For example: uint16_t*
to the new C++0x char16_t*
, or really any pointer to a basic data type that is the same size/alignment as the original type. Yet reinterpret_cast
provides no guarantees this should work.
Question: How can we safely convert between pointers to basic data-types of the same size/alignment, such as char*
--> unsigned char*
? Since reinterpret_cast
doesn't seem to guarantee this actually works, are C-style casts the only safe option here?
there's no guarantee that a reinterpret_cast from char* to unsigned char* won't crash your program when you try to dereference the unsigned char* pointer.
You can't do such a cast in any other way, so you have to have to trust what your compiler does with this completely reasonable cast.
Since reinterpret_cast doesn't seem to guarantee this actually works, are C-style casts the only safe option here?
The C-style cast will just map to reinterpret_cast
so it will be exactly the same. At some point you have to trust your compiler. The Standard has a limit on which it simply says "no. read your compiler's manual". When it comes to cross-casting pointers, this is such a point. It allows you to read a char
using an unsigned char
lvalue. A compiler that cannot cast a char*
to a usable unsigned char*
to do such is just about unusable and doesn't exist for that reason.
这篇关于reinterpret_cast在大多数情况下是没有用的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!