首先请务必读完cppreference上reinterpret_cast的描述
能读下来吗?
很难吧
记住了吗?
记不住吧
放心,没有几个人全都记得住的。所以不明白的东西不要用,尽量避免代码中出现reinterpret_cast
,因为它引发未定义行为的几率太高了
Whenever an attempt is made to read or modify the stored value of an object of type DynamicType through a glvalue of type AliasedType, the behavior is undefined unless one of the following is true:
- AliasedType and DynamicType are similar.
- AliasedType is the (possibly cv-qualified) signed or unsigned variant of DynamicType.
- AliasedType is std::byte, (since C++17)char, or unsigned char: this permits examination of the object representation of any object as an array of bytes.
常见的数据类型转换中,只有其它类型转成std::byte
,char
和unsigned char
才是安全的
简单来说&#