gpt4 book ai didi

c++ - C++14 是否定义了按位运算符在 unsigned int 的填充位上的行为?

转载 作者:可可西里 更新时间:2023-11-01 16:13:48 27 4
gpt4 key购买 nike

C++标准

如果 C++14实现包括 unsigned int 的底层字节中的填充位,标准是否指定是否不得对填充位执行按位操作?

此外,C++14 标准是否指定是否相等和关系运营商必须忽略填充位?

指南

如果在这方面缺乏规范,是否有某种就这些运算符在填充位上的预期行为达成共识?

我在 Stack Overflow 上发现了相互矛盾的答案。 Lightness Races in Orbitecatmur说按位运算符不适合算术,因为它们应用于所有位(包括填充位),而 ChristophBartek Banachewicz假设按位运算符处理整数的逻辑值并忽略填充。

引用资料

相关回答:关于padding bits的存在( 1 , 2 , 3 ),关于缺乏明确的 C++ 规范(4)。

C++14 中填充位的定义| - § 3.9.1 - 基本类型:

For narrow character types, all bits of the object representation participate in the value representation. For unsigned narrow character types, all possible bit patterns of the value representation represent numbers. These requirements do not hold for other types.

C++14 中对象表示和值表示的定义 - § 3.9 - 类型:

The object representation of an object of type T is the sequence of N unsigned char objects taken up by the object of type T, where N equals sizeof(T). The value representation of an object is the set of bits that hold the value of type T. For trivially copyable types, the value representation is a set of bits in the object representation that determines a value, which is one discrete element of an implementation-defined set of values.44

Footnote 44) The intent is that the memory model of C++ is compatible with that of ISO/IEC 9899 Programming Language C.

C++14 中按位与的定义 - § 5.11 - 按位与运算符:

The usual arithmetic conversions are performed; the result is the bitwise AND function of the operands. The operator applies only to integral or unscoped enumeration operands.

C++14 中加法的定义 - § 5.7 - 加法运算符:

The usual arithmetic conversions are performed for operands of arithmetic or enumeration type. For addition, [...] both operands shall have arithmetic or unscoped enumeration type [...]. The result of the binary + operator is the sum of the operands.

最佳答案

首先,C++ 标准本身几乎没有提及填充位。基本上所有关于填充位的讨论都来自基础文档(即 C 标准)。

所以真正的问题是 C 标准是怎么说的。它的脚注 54 对一般的填充位进行了相当简洁的总结:

Some combinations of padding bits might generate trap representations, for example, if one padding bit is a parity bit. Regardless, no arithmetic operation on valid values can generate a trap representation other than as part of an exceptional condition such as an overflow. All other combinations of padding bits are alternative object representations of the value specified by the value bits.

运营商可能会把 padding 改大。明显的情况是表示奇偶校验的填充位。如果您更改值的奇偶校验,奇偶校验位将更改以匹配。

“值的替代对象表示”部分基本上意味着只要您保持在“范围内”,填充位就不会影响您的结果。例如,如果您比较两个值,则仅使用表示位来确定结果(6.2.6.1/4):

Two values (other than NaNs) with the same object representation compare equal, but values that compare equal may have different object representations.

您必须注意的时间和地点大多涉及未定义或实现定义的行为。例如,如果您将一个值存储到 union 中的一个值中,然后在 union 中检索另一个值,则第二个可能会将填充位设置为陷阱表示,因此即使以这种方式查看值也可能使您的系统崩溃程序(或其他)。

同样,如果您要取两个值,memcpy,每个值到一个 unsigned char 缓冲区,这些字节的某些位可能比较不相等,即使它们表示的值确实比较相等.

即使您从不直接使用 mempy,这也会对您产生影响,这是一些比较和交换运算符。它们使用 memcpymemcmp 进行底层操作,因此即使所表示的值相等,它们也会比较不相等:

[atomics.types.operations]/23:

The memcpy and memcmp semantics of the compare-and-exchange operations may result in failed comparisons for values that compare equal with operator== if the underlying type has padding bits, trap bits, or alternate representations of the same value. Thus, compare_exchange_strong should be used with extreme care. On the other hand, compare_exchange_weak should converge rapidly.

旁注:两个大引号是描述性的,而不是规范性的——从规范性的角度来看,填充位几乎没有意义;几乎任何可能暴露填充位或其值的东西都涉及实现定义或未定义的行为。这里唯一的规范引用基本上是这样说的:“填充位无效。”

关于c++ - C++14 是否定义了按位运算符在 unsigned int 的填充位上的行为?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48331676/

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