static_cast和reinterpret_cast似乎都可以很好地将void*转换为另一种指针类型。是否有一个很好的理由来偏爱其中一个?


当前回答

static_cast更适合于将void*转换为其他类型的指针。

当两种类型之间存在自然的、直观的转换(不一定保证在运行时有效)时,可以选择Static_cast类型。例如,可以使用static_cast将基类指针转换为派生类指针,这种转换在某些情况下是有意义的,但直到运行时才能验证。类似地,可以使用static_cast将int类型转换为char类型,这是定义良好的,但在执行时可能会导致精度损失。

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 floats and ints aren't guaranteed to have anything in common with one another.

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*。

至于规范中更倾向于使用哪一种,这两种都没有被过分提及为“正确的使用”(或者至少,我不记得有哪一种是这样提到的)。然而,我认为规范希望你使用static_cast而不是reinterpret_cast。例如,当使用c风格强制转换时,例如

A* ptr = (A*) myVoidPointer;

尝试的强制转换操作符的顺序总是尝试在reinterpret_cast之前使用static_cast,这是您想要的行为,因为reinterpret_cast不能保证可移植。

其他回答

static_cast更适合于将void*转换为其他类型的指针。

当两种类型之间存在自然的、直观的转换(不一定保证在运行时有效)时,可以选择Static_cast类型。例如,可以使用static_cast将基类指针转换为派生类指针,这种转换在某些情况下是有意义的,但直到运行时才能验证。类似地,可以使用static_cast将int类型转换为char类型,这是定义良好的,但在执行时可能会导致精度损失。

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 floats and ints aren't guaranteed to have anything in common with one another.

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*。

至于规范中更倾向于使用哪一种,这两种都没有被过分提及为“正确的使用”(或者至少,我不记得有哪一种是这样提到的)。然而,我认为规范希望你使用static_cast而不是reinterpret_cast。例如,当使用c风格强制转换时,例如

A* ptr = (A*) myVoidPointer;

尝试的强制转换操作符的顺序总是尝试在reinterpret_cast之前使用static_cast,这是您想要的行为,因为reinterpret_cast不能保证可移植。

为此使用static_cast。只有在没有其他方法的极少数情况下才使用reinterpret_cast。

使用static_cast转换到void*和使用reinterpret_cast转换到void*是相同的。请在链接中查看答案。但是通常首选static_cast,因为它更窄,而且在一般情况下(但不是在这个特定的情况下)更安全的转换。

我建议尽量使用最弱的阵容。

Reinterpret_cast可用于强制转换指向浮点数的指针。类型转换越是破坏结构,使用它就越需要注意。

在char*的情况下,我会使用c风格的强制转换,直到我们有一些reinterpret_pointer_cast,因为它更弱,没有其他方法是足够的。

使用static_cast:它是最窄的强制转换,准确地描述了这里所进行的转换。

有一种误解,认为使用reinterpret_cast会是更好的匹配,因为它意味着“完全忽略类型安全,只是从a转换到B”。

然而,这实际上并没有描述reinterpret_cast的效果。相反,reinterpret_cast有许多含义,所有这些含义都认为“由reinterpret_cast执行的映射是实现定义的”。“(5.2.10.3)

但在从void*转换到T*的特殊情况下,该映射完全由标准定义;也就是说,将类型赋给无类型指针而不改变其地址。

这是首选static_cast的一个原因。

此外,可以说更重要的是,reinterpret_cast的每一次使用都是完全危险的,因为它实际上(对于指针)将任何东西转换为任何东西,而static_cast的限制要大得多,因此提供了更好的保护级别。这已经将我从错误中拯救出来,我不小心试图将一种指针类型强制转换为另一种。