With C++11, is it undefined behavior to write f(x++), g(x++)?

No, the behavior is defined. To quote C++11 (n3337) [expr.comma/1]:

A pair of expressions separated by a comma is evaluated left-to-right;
the left expression is a discarded-value expression (Clause [expr]).
Every value computation and side effect associated with the left
expression is sequenced before every value computation and side effect
associated with the right expression.

And I take “every” to mean “every”1. The evaluation of the second x++ cannot happen before the call sequence to f is completed and f returns.2



1 Destructor calls aren’t associated with sub-expressions, only with full expressions. So you’ll see those executed in reverse order to temporary object creation at the end of the full expression.
2 This paragraph only applies to the comma when used as an operator. When the comma has a special meaning (such when designating a function call argument sequence) this does not apply.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)