Sequence Point and Evaluation Order( Preincrement)

Posted by Josh on Stack Overflow See other posts from Stack Overflow or by Josh
Published on 2014-08-20T10:13:15Z Indexed on 2014/08/20 10:20 UTC
Read the original article Hit count: 181

Filed under:
|
|

There was a debate today among some of my colleagues and I wanted to clarify it. It is about the evaluation order and the sequence point in an expression. It is clearly stated in the standard that C/C++ does not have a left-to-right evaluation in an expression unlike languages like Java which is guaranteed to have a sequencial left-to-right order. So, in the below expression, the evaluation of the leftmost operand(B) in the binary operation is sequenced before the evaluation of the rightmost operand(C):

A = B B_OP C

The following expression according, to CPPReference under the subsection Sequenced-before rules(Undefined Behaviour) and Bjarne's TCPPL 3rd ed, is an UB

x = x++ + 1;

It could be interpreted as the compilers like BUT the expression below is said to be clearly a well defined behaviour in C++11

x = ++x + 1;

So, if the above expression is well defined, what is the "fate" of this?

array[x] = ++x;

It seems the evaluation of a post-increment and post-decrement is not defined but the pre-increment and the pre-decrement is defined.

NOTE: This is not used in a real-life code. Clang 3.4 and GCC 4.8 clearly warns about both the pre- and post-increment sequence point.

© Stack Overflow or respective owner

Related posts about c++

Related posts about c++11