C++ 中函数参数的顺序
我正在阅读“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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
函数参数的求值顺序未指定,但表达式的行为未定义。比亚恩刚刚搞错了。 :-)
查看此线程以了解未指定行为和未定义行为之间的区别行为。
我曾经问 Bjarne 未指定和未定义之间的区别,这是他的答复。
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
andundefined
and this was his reply..我认为我们在这里迷失了语义。规范就是直接或省略的定义。许多未指定的行为被留下来允许通过实现定义的行为进行优化,这会导致未定义的行为,至少如果您将代码移动到不同的编译器或硬件,因此您的代码失去了通用性,并且也可能失去了清晰度和简单性。正如 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". :)