C++03 和 C++11 之间的未定义行为有什么区别?

发布于 2024-10-06 04:27:35 字数 105 浏览 9 评论 0原文

新标准与旧标准具有不同的未定义行为。例如,新的排序规则意味着一些过去未定义的算术运算(由于序列点之间的多次写入等原因)现在已定义。

那么,对于未定义的行为,我们需要重新学习什么呢?

The new standard has different undefined behavior from the old one. The new sequencing rules, for example, mean that some arithmetic operations that used to be undefined (for such reasons as multiple writes between sequence points) are now defined.

So, what do we need to learn anew about undefined behavior?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

囚你心 2024-10-13 04:27:35

在我看来,新规则的描述和理解更加复杂。例如,考虑一下:

int x = 12;
x = x++ + 1; // undefined behaviour
x = ++x + 1; // valid

我建议简单地避免同一表达式中同一变量的多个副作用,这是一条更容易理解的规则。 AFAIK C++0X 改变了一些过去未定义行为但现在合法使用的情况(例如上面两个表达式中的第二个),但请记住,合法和合法之间始终存在差异是道德的;-) ...没有人强迫你使用这样的东西。

实际上,在上述情况下,第二个表达式的有效性似乎是无意中发生的,是修复语言中另一个问题(#222)的副作用。我们决定使该表达式有效,因为人们认为将某些内容从 UB 更改为 well Defined 不会造成任何损害。然而,我认为,虽然这不会对程序造成任何损害(当然,UB 是最糟糕的问题),但它实际上对语言本身造成了一些损害……改变了一个已经很复杂的解释和理解规则更晦涩难懂的一个。

IMO C++ 正在继续其从 C 的自然演变,成为一种语言,其中一堆好看、漂亮、逻辑性强的语句可以做奇妙的事情……而在这种语言中,另一堆同样好看、同样漂亮、同样逻辑性的语句可以让你的电脑反而会爆炸。

In my opinion the new rules are more complex to describe and to understand. For example consider that:

int x = 12;
x = x++ + 1; // undefined behaviour
x = ++x + 1; // valid

I would suggest to simply avoiding multiple side effects to the same variable in the same expression that is a rule simpler to understand. AFAIK C++0X changed some cases that were undefined behaviour in the past and that are now legal uses (for example the second of the two expressions above) but remember that there is and there will always be a difference between what is legal and what is moral ;-) ... no one is forcing you to use such things.

Actually in the above case seems that the validity of the second expression happened unintentionally as a side effect of fixing another issue (#222) in the language. The decision was to make the expression valid because it was considered that changing something from UB to well defined wasn't going to do any harm. I think however that while this didn't make any damage to programs (where of course UB is the worst possible problem), it actually made some damage to the language itself... changing a rule that was already complex to explain and understand to an even more obscure one.

IMO C++ is continuing its natural evolution from C into a language where a bunch of good-looking nice and logical statements can do wonderful things... and in which another bunch of equally good-looking, equally nice and equally logical statements can make your computer to explode instead.

来日方长 2024-10-13 04:27:35

C++0x 将许多以前未定义的情况更改为现在有条件支持的情况。语义是:

  • 如果实现不支持有条件支持的功能,则它应记录该情况,并对违反该功能的程序发出诊断。
  • 如果实现确实支持它,它应该满足标准对其提出的附加要求。例如,标准可能会说某些东西是通过实现定义的语义有条件支持的。如果是这样,实现应记录它如何支持该功能。

以前未定义的一个常见情况是,通过省略号函数参数传递具有非平凡复制构造函数、非平凡移动构造函数或非平凡析构函数的类类型参数时。现在这是有条件支持的,具有实现定义的语义。

C++0x changes a number of previously undefined cases to now conditionally-supported cases. The semantics is:

  • If the implementation does not support the conditionally-supported feature, it shall document that and shall emit a diagnostic for a program that violates it.
  • If the implementation does support it, it should behave to additional requirements the Standard makes on it. For example, the Standard might say something is conditionally-supported with implementation-defined semantics. If so, the implementation shall document how it supports the feature.

A popular case that previously was undefined is when passing an argument of class type having a non-trivial copy constructor, a non-trivial move contructor, or a non-trivial destructor though an ellipsis function parameter. This is now conditionally-supported, with implementation-defined semantics.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文