8

首先,这不是重复为什么我们在 C++ 中有 reinterpret_cast 当两个链式 static_cast 可以完成它的工作?.

我知道我们甚至不能使用两个链式static_cast来实现这一点的情况,是什么reinterpret_cast。但是在任何情况下,我应该更喜欢两个链接static_cast而不是简单且更具可读性reinterpret_cast吗?

4

6 回答 6

9

reinterpret_cast应该是一个巨大的闪烁符号,上面写着这看起来很疯狂,但我知道我在做什么。不要因为懒惰而使用它。

reinterpret_cast意味着“将这些位视为......” 链式静态强制转换一样,因为它们可能会根据继承格修改它们的目标。

struct A {
    int x;
};

struct B {
    int y;
};

struct C : A, B {
    int z;
};

C c;
A * a = &c;

int main () {
    assert (reinterpret_cast <B *> (a) != static_cast <B *> (static_cast <C *> (a)));
}

如果您不是 100% 确定a指向 a b,请使用dynamic_castwhich 将搜索上述解决方案(尽管会产生运行时成本)。请记住,这可能会返回 NULL 或抛出失败。

我试着想想我实际使用过的时间reinterpret_cast,实际上只有两个:

  • 当一个函数正在压缩/加密任意缓冲区并且我想使用 aconst char *来遍历它时
  • if(*reinterpret_cast<uint32_t*>(array_of_4_bytes_A) < *reinterpret_cast<uint32_t*>(array_of_4_bytes_B)或类似的。像这样的行会引起审查并要求评论。

否则,如果你有一个A*真的是一个,B*那么你可能想要一个工会。

于 2011-07-06T09:50:18.367 回答
5

I for one would rather see reinterpret_cast <TargetType> (pointer_of_some_other_type) than static_cast <TargetType> (static_cast <void*> (pointer_of_some_other_type)) or static_cast <TargetType> ((void*) (pointer_of_some_other_type)) any time. That chain of casts going through void* is just a sneaky, underhanded way to avoid using the dreaded reinterpret_cast.

Many projects ban the use of reinterpret_cast unless a waiver is granted; the person who wrote the code needs to justify the use of the cast. IMO, a chain of static casts is worse (much worse!) than is reinterpret_cast. The chain has the same effects, the same problems as does a reinterpret_cast, but the chain does not have the benefit of being easy to find with a grep.

Addendum
Look at it this way. Case 1, you use reinterpret_cast, you go through all the project hoops to justify its use, the project manager grants a waiver. Months later, an error is traced to your use of dynamic_cast. You have a get out of jail free card. It is the project manager's ass that is on the line for giving you that card.

Case 2, you use the sneaky, underhanded chain of static casts and the code sneaks through peer review unscathed. Months later, an error is traced to your use of despicable technique. Your project manager might be in a bit of trouble for not catching this nastiness, but it is your ass that is on the line. You do not have that get out of jail free card. You do not pass Go. You go directly to the unemployment line.

于 2011-07-06T10:27:35.200 回答
2

始终使用 reinterpret cast 作为最后的手段——它不做任何检查!- 因此,如果您可以将两个、三个或十个语句链接在一起,对操作进行某种验证,那么您就获得了一些有价值的东西。

于 2011-07-06T09:49:20.087 回答
2

由于场景列表可能会很长,所以我用简单的话来说:

如果 chainedstatic_cast<>不给出编译错误,你应该避免reinterpret_cast<>.

于 2011-07-06T09:49:27.397 回答
1

您不应该在交叉转换指针的情况下使用 - 而是使用隐式转换为,reinterpret_castvoid* then static_cast

于 2011-07-06T09:49:03.697 回答
0

因为理论上,他们可以做一些不同的事情(尽管很难想象这样的情况)。更重要的是,它们向读者发送不同的信号,并向编译器讲述不同的故事(这可能会影响优化)。从逻辑上讲,我会说使用链接static_castvoid*/来自字符类型的案例(例如转储原始内存的图像),以及其他定义明确且可移植的案例,以及reinterpret_cast当你在做真正的低级别时,硬件依赖工作:例如,float通过将其地址转换为 aunsigned int*和位掩码来提取 a 的指数字段。

于 2011-07-06T10:32:01.013 回答