【发布时间】:2018-07-04 13:36:52
【问题描述】:
如Is right shift undefined behavior if the count is larger than the width of the type? 中所述,如果移位次数超过有效操作数大小,则移位值未定义。
因此,在下面,bar 的值是未定义的:
uint32_t foo = 123;
uint32_t bar = (foo >> 33);
这种移位操作是否为std::bitset 定义得很好?如:
std::bitset<32> foo(123);
std::bitset<32> bar(foo >> 33);
在哪个官方文档中可以找到这些信息?
cppreference (https://en.cppreference.com/w/cpp/utility/bitset/operator_ltltgtgt) 上没有明确说明这种情况。
【问题讨论】:
-
请注意
undefined behavior与implementation-defined result不同。前者实际上可以使任何事情发生,后者确保结果记录在您正在使用的编译器/体系结构中(如果它是兼容的)。例如,sizeof int是实现定义的,*nullptr是未定义的 -
@pqnet:我们可能都知道你对最后一个的意思是什么,但我真的想指出
*nullptr格式不正确,而*(int*)nullptr是UB。
标签: c++ c++11 c++14 language-lawyer bit-shift