skip to content

In C++17 and later, which of i = i++ + 1, i++ + i++ and f(i++, i++) have undefined behaviour, and why?

level: middleimportance: should knowfreq 55%

answer

  1. sequenced, indeterminate, unsequenced
  2. two side effects, one object
  3. C++17: right of = goes first
  4. arguments never interleave

basics

~20 s

Only i++ + i++ is undefined: the two operands of + are unsequenced and both modify i. Since C++17, i = i++ + 1 is defined, and f(i++, i++) is only unspecified, because arguments are indeterminately sequenced.

solid answer

~40 s

The rule: if a side effect on a memory location is **unsequenced** relative to another side effect or a value computation on the same location, the behaviour is undefined. Operands of `+` are unsequenced, so `i++ + i++` is undefined in every standard. C++17 sequenced the right operand of `=` before the left, so `i = i++ + 1` became defined: `i` ends one greater than before. Function arguments are **indeterminately sequenced** since C++17: each runs completely before or after the other in an unspecified order, so `f(i++, i++)` passes 1 and 2 in some order, which is unspecified behaviour, not undefined. C++17 also orders `<<`, `>>` and subscripts left to right, so `std::cout << i++ << i++` is defined.

code

cpp · 18 lines
cpp
#include <iostream>

void show(int a, int b) { std::cout << a << ' ' << b << '\n'; }

int main() {
    int i = 1;
    i = i++ + 1;              // C++17: defined, i becomes 2
    std::cout << i << '\n';

    int a[3] = {7, 7, 7};
    int m = 0;
    a[m] = m++;               // C++17: right side first, so a[1] = 0
    std::cout << a[0] << a[1] << '\n';

    int j = 1;
    show(j++, j++);           // unspecified order: "1 2" or "2 1"
    // int r = j++ + j++;     // undefined in every standard
}

go deeper

for a junior

Remember the safe habit: never modify the same variable twice, or modify and read it, inside one expression.

for a middle

Separate unsequenced, indeterminately sequenced and sequenced-before, and classify i = i++ + 1, i++ + i++ and f(i++, i++) correctly for C++17.

for a senior

Point out code that is defined in C++17 but undefined when the same header builds as C++14, and why a warning can over-report or under-report.

for a principal

Treat order-dependent expressions as a review rule rather than a language-lawyer debate, since defined-but-unspecified results are still fragile.

## Three kinds of "the standard doesn't say" Interviewers use this question to see whether a candidate separates three terms the standard defines precisely: - **Implementation-defined**: one behaviour, chosen and documented by each implementation, such as `sizeof(int)`. - **Unspecified**: one of several allowed behaviours, not documented and possibly different from run to run, such as the order in which two function arguments are evaluated. - **Undefined**: no requirements at all, such as two unsequenced writes to one variable. An unspecified order gives you one of a known set of answers. An undefined one gives you no answer. ## The sequencing vocabulary Inside one thread, evaluations relate in one of three ways: 1. **Sequenced before**: A completes, value and side effects, before B starts. 2. **Indeterminately sequenced**: one completes entirely before the other, but the standard does not say which. 3. **Unsequenced**: the two may overlap or interleave in any way. Unless a rule says otherwise, the operands of an operator are **unsequenced**. The undefined-behaviour rule is stated in terms of memory locations: if a side effect on a memory location is unsequenced relative to another side effect on the same location, or to a value computation that uses the value stored there, the behaviour is undefined. The draft's own example lists `i = i++ + 1;` as incrementing `i` and `i = i++ + i;` as undefined behaviour. ## What C++17 changed C++17 added sequencing rules that turned several classic undefined expressions into defined or merely unspecified ones: | Expression | C++14 | C++17 and later | |---|---|---| | `i = i++ + 1` | undefined | defined: the right operand is sequenced before the left | | `a[m] = m++` | undefined | defined: right side first, so it assigns `a[1] = 0` from `m == 0` | | `std::cout << i++ << i++` | undefined | defined: `E1` before `E2` for `<<` and `>>`, also for overloaded operators | | `f(i++, i++)` | undefined | unspecified: arguments are indeterminately sequenced | | `i++ + i++` | undefined | **still undefined**: operands of `+` are unsequenced | | `i = i++ + i` | undefined | **still undefined**: the read of `i` on the right is unsequenced with `i++` | For function calls the rule is that the initialization of each parameter, including every side effect, is indeterminately sequenced with respect to every other parameter. The two `i++` can no longer interleave, so `f` receives 1 and 2, in an order you must not rely on. ## Compilers lag behind the rule Diagnostics are a guide, not the definition. Apple clang 21 with `-Wall` still reports `multiple unsequenced modifications to 'j' [-Wunsequenced]` for `show(j++, j++)` in `-std=c++17` mode, although the standard calls that only unspecified. The warning is still worth heeding: code whose result depends on argument order is fragile even when it is not undefined. ## Classifying an expression at the whiteboard A reliable procedure beats memorising a table: 1. List every object the expression **modifies**: each `++`, `--`, assignment and compound assignment. 2. For each such object, find every other access to it, whether a read or another write, in the same full expression. 3. For each pair, find the operator that separates them and ask what it guarantees: `+`, `*`, `==` and most other operators leave their operands unsequenced; `&&`, `||`, the comma operator and `?:` sequence their first operand first; since C++17 `=`, `<<`, `>>`, `[]` and `->*` sequence one side before the other; function arguments are indeterminately sequenced. 4. An unsequenced pair touching the same object makes the expression undefined; an indeterminately sequenced pair makes the result unspecified; otherwise it is defined. ## Writing it so nobody has to know the rules - Put **at most one side effect per variable** in a full expression; split `i = i++ + 1` into `++i;` or `i += 1;`. - Never pass two arguments that modify, or modify and read, the same object to one call. - Do not rely on C++17's ordering in code that must also build as C++14, where the same lines are undefined. - Keep the warning enabled; the material on compiler warnings covers it. The general sequenced-before rules belong with the lesson on operators and sequencing; what matters here is the line between the one form that is still undefined and the forms C++17 rescued.

  • Why is i = i++ + i still undefined in C++17 when i = i++ + 1 is not?
    C++17 sequences the whole right operand before the assignment, but inside the right operand the two operands of `+` are still unsequenced. One operand modifies `i` through `i++`, the other reads `i`, and an unsequenced side effect and value computation on the same object are undefined. In `i = i++ + 1` the right operand touches `i` only once.
  • Is std::cout << i++ << i++ defined in C++17?
    Yes. C++17 sequences `E1` before `E2` for the shift operators, and an overloaded operator keeps the sequencing of the built-in one. So the left `i++` and its output happen before the right one, and the stream prints the old value followed by the incremented one. Before C++17 the same line was undefined.

  • f(i++, i++) is undefined behaviour in C++17 because both arguments modify i.
  • i = i++ + 1 is undefined in every C++ standard.
  • Undefined and unspecified mean the same thing: the compiler picks something.
  • C++17 made evaluation left to right everywhere, so i++ + i++ is now defined.
  • If the compiler prints no warning, the expression has no sequencing problem.

Assumes C++17 or later; C++17 sequenced the right operand of = before the left and made function arguments indeterminately sequenced.