C++ 中函数参数的顺序

发布于 2024-10-13 07:23:07 字数 343 浏览 7 评论 0原文

我正在阅读“Bjarne Stroustrup 的 C++ 风格和技术”常见问题解答,他在其中提到了此常见问题解答 i++ + i++ 的价值是什么?

在此他有提到“...函数参数的求值顺序未定义。”当他指的是 f(v[i],i++);例子。

我知道函数参数的求值顺序未指定不是未定义的,但在这种情况下它是未定义的,因为我们依赖 i 的值来传递哪个 v[i] 或其常见问题解答本身有错误吗?

I was reading "Bjarne Stroustrup's C++ Style and Technique" FAQ where he mentioned about this FAQ
What's the value of i++ + i++?

In this he has mentioned "...the order of evaluation of function arguments are undefined." when he is refering to f(v[i],i++); example.

I am aware of that the order of evaluation of function arguments is unspecified not undefined but in this case is it undefined because we are relying on value of i so as to which v[i] to pass or its an error in the FAQ itself?

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

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

发布评论

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

评论(2

优雅的叶子 2024-10-20 07:23:07

函数参数的求值顺序未指定,但表达式的行为未定义。比亚恩刚刚搞错了。 :-)

查看线程以了解未指定行为和未定义行为之间的区别行为。

我曾经问 Bjarne 未指定和未定义之间的区别,这是他的答复。

就我个人而言,我并不关心“未定义”和“未指定”之间的区别;你知道那是什么吗?无论哪种情况,都应该取出并拍摄该代码:(小心避免使用“未定义”和“未指定”一词)

......

您不需要知道:只需避免两者即可。如果有人真的想知道,他们可以查找 C++ 或 C 标准中的定义(我敢打赌,C 和 C++ 标准在不重要的细节上会存在分歧)。

The order of evaluation of function arguments is unspecified but the behaviour of the expression is undefined. Bjarne has just got it wrong. :-)

Check out this thread to understand the difference between Unspecified Behaviour and Undefined Behaviour.

I once asked Bjarne the difference between unspecified and undefined and this was his reply..

Personally, I don't bother with the distinction between "undefined" and "unspecified"; do you know what it is? That code should be taken out and shot in either case: (carefully avoiding the words "undefined" and "unspecified")

.....

You don't need to know: just avoid both. If someone really want to know they can look up the definitions in the C++ or C standards (my bet would be that the C and C++ standards will disagree on unimportant details).

痴梦一场 2024-10-20 07:23:07

我认为我们在这里迷失了语义。规范就是直接或省略的定义。许多未指定的行为被留下来允许通过实现定义的行为进行优化,这会导致未定义的行为,至少如果您将代码移动到不同的编译器或硬件,因此您的代码失去了通用性,并且也可能失去了清晰度和简单性。正如 Brian Kernighan 所说,试图利用潜在“副作用”技巧的代码通常不是好风格。但看到有人指出 Bjarne Stroustrup 的“错误”很有趣。 :)

I think we are lost in semantics here. Specification is definition, directly or by omission. Much of unspecified behavior was left to allow optimization through implementation-defined behavior, what leads to undefined behavior, at least if you move your code to different compilers or hardware, so your code loses generality, and it also has probably lost clarity and simplicity. Like Brian Kernighan also says, code which try to take advantage of potential "side effect" artifices is often not good style. But it was funny see somebody pointing Bjarne Stroustrup "mistakes". :)

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