开发者

Order of Function arguments in C++

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 o开发者_如何学Pythonrder 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?


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).


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". :)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜