In C and C++, a sequence point defines any point in a computer program's execution at which it is guaranteed that all side effects of previous evaluations will have been performed, and no side effects from subsequent evaluations have yet been performed. They are a core concept for determining the validity of and, if valid, the possible results of expressions. Adding more sequence points is sometimes necessary to make an expression defined and to ensure a single valid order of evaluation.
Documentation for C11 and C++11 stopped using the term "sequence point" and now uses alternative terms:
The execution of unsequenced evaluations can overlap, leading to potentially catastrophic undefined behavior if they share state. This situation can arise in parallel computing, causing race conditions, but undefined behavior can also result in single-threaded situations. For example, <code>a[i] = i++;</code> (where is an array and is an integer) has undefined behavior.
Consider two functions <code>f()</code> and <code>g()</code>. In C and C++, the <code>+</code> operator is not associated with a sequence point, and therefore in the expression <code>f()+g()</code> it is possible that either <code>f()</code> or <code>g()</code> will be executed first. The comma operator introduces a sequence point, and therefore in the code <code>f(),g()</code> the order of evaluation is defined: first <code>f()</code> is called, and then <code>g()</code> is called.
Sequence points also come into play when the same variable is modified more than once within a single expression. An often-cited example is the C expression <code>i=i++</code>, which apparently both assigns <code>i</code> its previous value and increments <code>i</code>. The final value of <code>i</code> is ambiguous, because, depending on the order of expression evaluation, the increment may occur before, after, or interleaved with the assignment. The definition of a particular language might specify one of the possible behaviors or simply say the behavior is undefined. In C and C++, evaluating such an expression yields undefined behavior. Other languages, such as C#, define the precedence of the assignment operator and the increment operator in such a way that the result of the expression <code>i=i++</code> is guaranteed.
In C and C++, sequence points occur in the following places. (In C++, overloaded operators act like functions, and thus operators that have been overloaded introduce sequence points in the same way as function calls.)
C++11 brought diverse changes, including features that improve the language's performance, usability, and multithreading. Partially because of the introduction of language support for threads, C11 and C++11 introduced new terminology for evaluation order. An operation may be âÂÂsequenced beforeâ another, the two can be âÂÂindeterminately sequencedâ (one must complete before the other), or the two can be âÂÂunsequencedâ (the operations in each expression may be interleaved).
C++17 restricted several aspects of evaluation order. The expression will always perform the memory allocation before evaluating the constructor arguments. The operators , , , , , and the subscript and function call operator are guaranteed to be evaluated left to right (whether they are overloaded or not). For example, the code
is newly guaranteed to call , , and in that order. The right-hand side of any assignment-like operator is evaluated before the left-hand side, so that <code>b() *= a();</code> is guaranteed to evaluate first. Finally, although the order in which function parameters are evaluated remains implementation-defined, the compiler is no longer allowed to interleave sub-expressions across multiple parameters.