gpt4 book ai didi

c++ - 写入有符号整数,就好像它在 C++ 中是无符号的一样

转载 作者:太空狗 更新时间:2023-10-29 20:53:50 24 4
gpt4 key购买 nike

reinterpret_cast这样做是安全的,这是最好的方法吗?

例如,在下面的代码中,我有一个名为 ibytestream 的类, 允许读取 uint16_t s 和 int16_t从它。 ibytestream::nextvector<unsigned char>::iterator .

inline ibytestream& operator>>(ibytestream& stream, uint16_t& data) {
data = 0;
data |= *stream.next++;
data <<= 8;
data |= *stream.next++;
return stream;
}

inline ibytestream& operator>>(ibytestream& stream, int16_t& data) {
return stream >> reinterpret_cast<uint16_t&>(data);
}

我不想重复将字节转换为整数的代码,所以我使用了 reinterpret_cast签名版本重用未签名版本的代码。它在我的机器上运行良好,但它在其他现代机器上是否普遍运行?

最佳答案

是的,这是安全的。

标准的三个部分适用于做出此决定:

  1. 有符号和无符号类型的对齐要求相同
  2. 允许在指向具有相同对齐要求的类型的指针之间进行指针转换
  3. 执行左值之间的转换时,如果相应指针之间的转换有效,则转换有效。

For each of the standard signed integer types, there exists a corresponding (but different) standard unsigned integer type: unsigned char, unsigned short int, unsigned int, unsigned long int, and unsigned long long int, each of which occupies the same amount of storage and has the same alignment requirements.

An object pointer can be explicitly converted to an object pointer of a different type. Converting a prvalue of type “pointer to T1” to the type “pointer to T2” (where T1 and T2 are object types and where the alignment requirements of T2 are no stricter than those of T1) and back to its original type yields the original pointer value.

A glvalue expression of type T1 can be cast to the type “reference to T2” if an expression of type “pointer to T1” can be explicitly converted to the type “pointer to T2” using a reinterpret_cast. The result refers to the same object as the source glvalue, but with the specified type.

关于c++ - 写入有符号整数,就好像它在 C++ 中是无符号的一样,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41297601/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com