将按位移位运算符应用于有符号类型:UB 和 Impl。定义的
C++03标准告诉我们,将按位移位运算符应用于有符号类型的结果可以是UB和Impl。定义为负值。我的问题如下:为什么对于运算符<<
它有未定义的行为,而对于运算符>>
它只是 > 实现定义 ? <<
的结果不能也被实现定义是否有严格的原因?
提前致谢。
C++03 standard tells us that the result of applying the bitwise shift operators to signed types can be UB and Impl. defined for negative values. My question is following: why for operator <<
it has undefined behaviour, while for operator >>
it is just implementation defined ? Is there a strict reason why the result of <<
couldn't be implementation defined also ?
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据 5.8/2(诚然,在 C++ 98 中,这是我可以访问的所有内容):
从这里看来,在我看来,它对于左移来说是完美定义的。未定义的是所使用的有符号值(例如二进制补码)的表示形式,因此结果的数字值是为负值定义的实现。
这与右移相反,右移根据有符号值的表示,空出的位可能为零或被填充。
According to 5.8/2 (admittedly in C++ 98 which is all I have access to):
From this it looks to me like it's perfectly well defined for left shift. What's not defined is the representation of signed values (such as twos-complement) used so the numeric value of the result is implementation defined for negative values.
This is in contrast to right-shifting where the vacated bits may be zero or one filled depending on the representation of signed values.