Why Doesn't reinterpret_cast Force copy_n for Casts between Same-Sized Types?

后端 未结 2 1169
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-30 15:20

According to cppreference.com, reinterpret_cast:

Converts between types by reinterpreting the underlying bit pattern.

2条回答
  •  没有蜡笔的小新
    2020-11-30 16:12

    Mainly the restrictions on reinterpret_cast (not fully captured by the cppreference site) are because of

    • alignment issues and
    • trap representations.

    For a hypothetical reinterpret_cast to number value, different sizes can easily be handled by truncation or zero-extension, since one is into dangerous bit-level territory anyway, so that's not an issue.

    By using memcpy or copy_n you work around the alignment issues, but you are still possible victim of trap representations. That means that use of the resulting value might blow up. On some platforms, for some values.


    Note that the standard's guarantees about anything, can be and usually are extended by any particular compiler.

    Often it can be a good idea to aim for just a little less portability than what you can have by relying solely on the standard.

    For example, things get complicated fast when you can't assume that a byte is 8 bits. Making this assumption reduces portability. But still the set of supported platforms is large.

提交回复
热议问题