将void指针转换为特定类型的指针时,static_cast或reinterpret_cast是哪个转换符号更好? [英] When convert a void pointer to a specific type pointer, which casting symbol is better, static_cast or reinterpret_cast?
问题描述
在此程序中,我有一个 void *
作为参数,并希望将其强制转换到特定类型。但我不知道要使用哪个铸造符号。 static_cast
或 reinterpret_cast
均有效。哪一个更好?标准C ++推荐哪个?
In this program I have a void *
as a parameter and want to cast it to a specific type. But I don't know which "casting symbol" to use. Both static_cast
or reinterpret_cast
work. Which one is better? Which one does the Standard C++ recommend?
typedef struct
{
int a;
}A, *PA;
int foo(void* a) // the real type of a is A*
{
A* pA = static_cast<A*>(a); // or A* pA = reinterpret_cast<A*>(a);?
return pA->a;
}
在这里,是
A* pA = static_cast<A*>(a);
或
A* pA = reinterpret_cast<A*>(a);
更合适吗?
推荐答案
static_cast
更适合将 void *
转换为其他类型的指针
The static_cast
is more appropriate for converting a void*
to a pointer of some other type.
static_cast
是两种类型之间自然而直观的转换,而不是必须保证可以在运行时工作。例如,您可以使用 static_cast
将基类指针转换为派生类指针,这种转换在某些情况下是有意义的,但要等到运行时才能得到验证。同样,您可以使用 static_cast
将 int
转换为 char
,定义明确,但执行时可能会导致精度损失。
static_cast
is the cast of choice when there is a natural, intuitive conversion between two types that isn't necessarily guaranteed to work at runtime. For example, you can use static_cast
to convert base class pointers to derived class pointers, which is a conversion that makes sense in some cases but can't be verified until runtime. Similarly, you can use static_cast
to convert from an int
to a char
, which is well-defined but may cause a loss of precision when executed.
reinterpret_cast
,在另一方面,是一位铸造操作员,旨在进行根本上不安全或不便于携带的转换。例如,您可以使用 reinterpret_cast
将 void *
转换为 int
,如果您的系统恰好具有 sizeof(void *)
≤ sizeof(int)
。您还可以使用 reinterpret_cast
将 float *
转换为 int *
或相反,这是特定于平台的,因为 float
s和 int
s的特定表示形式
reinterpret_cast
, on the other hand, is a casting operator designed to do conversions that are fundamentally not safe or not portable. For example, you can use reinterpret_cast
to convert from a void *
to an int
, which will work correctly if your system happens to have sizeof (void*)
≤ sizeof (int)
. You can also use reinterpret_cast
to convert a float*
to an int*
or vice-versa, which is platform-specific because the particular representations of float
s and int
s aren't guaranteed to have anything in common with one another.
简而言之,如果您发现自己进行的转换在逻辑上是有意义的,但不一定成功在运行时,请避免 reinterpret_cast
。如果您事先知道强制转换将在运行时工作,并与编译器进行通信,则 static_cast
是一个不错的选择,至少有道理,我有理由相信它会在运行时正确地做正确的事情。然后,编译器可以检查转换是否在相关类型之间,如果不是这种情况,则报告编译时错误。使用 reinterpret_cast
进行指针转换可以完全绕过编译时安全检查。
In short, if you ever find yourself doing a conversion in which the cast is logically meaningful but might not necessarily succeed at runtime, avoid reinterpret_cast
. static_cast
is a good choice if you have some advance knowledge that the cast is going to work at runtime, and communicates to the compiler "I know that this might not work, but at least it makes sense and I have a reason to believe it will correctly do the right thing at runtime." The compiler can then check that the cast is between related types, reporting a compile-time error if this isn't the case. Using reinterpret_cast
to do this with pointer conversions completely bypasses the compile-time safety check.
有几种情况您可能想使用 dynamic_cast
而不是 static_cast
的位置,但是这些大多涉及类层次结构中的强制转换和(很少)直接关注 void *
。
There are a few circumstances where you might want to use a dynamic_cast
instead of a static_cast
, but these mostly involve casts in a class hierarchy and (only rarely) directly concern void*
.
对于规范首选的对象,都没有过多提及作为使用的合适人选(或者至少我不记得有人提到过这种方式。)但是,我认为规范要求您使用 static_cast
通过 reinterpret_cast
。例如,使用C样式的强制转换时,例如
As for which one is preferred by the spec, neither is overly mentioned as "the right one to use" (or at least, I don't remember one of them being mentioned this way.) However, I think the spec wants you to use static_cast
over reinterpret_cast
. For example, when using a C-style cast, as in
A* ptr = (A*) myVoidPointer;
尝试使用的转换运算符顺序始终尝试使用 static_cast
在 reinterpret_cast
之前,这是您想要的行为,因为不能保证 reinterpret_cast
是可移植的
The order of casting operators that's tried always tries to use a static_cast
before a reinterpret_cast
, which is the behavior you want since reinterpret_cast
isn't guaranteed to be portable.
这篇关于将void指针转换为特定类型的指针时,static_cast或reinterpret_cast是哪个转换符号更好?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!