问题描述
我的理解是C ++ reinterpret_cast和C指针强制转换只是
a的编译时功能,根本没有性能成本。
My understanding is that C++ reinterpret_cast and C pointer cast is a justa compile-time functionality and that it has no performance cost at all.
是
推荐答案
这是一个很好的假设。但是,在存在 reinterpret_cast<>
或C指针强制转换的情况下,优化器可能会受到限制。然后,即使强制类型转换本身没有关联的指令,生成的代码也会变慢。
It's a good assumption to start with. However, the optimizer may be restricted in what it can assume in the presence of a reinterpret_cast<>
or C pointer cast. Then, even though the cast itself has no associated instructions, the resulting code is slower.
例如,如果将int强制转换为指针,则优化器可能会具有不知道该指针可能指向什么。结果,可能必须假设通过该指针进行的写操作可以更改任何变量。优于非常常见的优化方法,例如将变量存储在寄存器中。
For instance, if you cast an int to a pointer, the optimizer likely will have no idea what that pointer could be pointing to. As a result, it probably has to assume that a write through that pointer can change any variable. That beats very common optimizations such as storing variables in registers.
这篇关于reinterpret_cast转换费用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!