Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Sequence point
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
==Behavior== === Up to C++03 === In C<ref>Annex C of the [[C99]] specification lists the circumstances under which a sequence point may be assumed.</ref> and C++,<ref>The 1998 C++ standard lists sequence points for that language in section 1.9, paragraphs 16–18.</ref> sequence points occur in the following places. (In C++, [[operator overloading|overloaded operators]] act like functions, and thus operators that have been overloaded introduce sequence points in the same way as function calls.) #Between evaluation of the left and right operands of the <code>&&</code> ([[logical conjunction|logical AND]]), <code>||</code> ([[logical disjunction|logical OR]]) (as part of [[short-circuit evaluation]]), and [[comma operator]]s. For example, in the expression {{code|2=c|1=*p++ != 0 && *q++ != 0}}, all side effects of the sub-expression {{code|2=c|1=*p++ != 0}} are completed before any attempt to access {{code|2=c|1=q}}. #Between the evaluation of the first operand of the [[ternary conditional operator]] and its second or third operand. For example, in the expression {{code|2=c|1=a = (*p++) ? (*p++) : 0}} there is a sequence point after the first {{code|2=c|1=*p++}}, meaning it has already been incremented by the time the second instance is executed. #At the end of a full expression. This category includes expression statements (such as the assignment {{code|2=c|1=a=b;}}), [[return statement]]s, the controlling expressions of {{code|2=c|1=if}}, {{code|2=c|1=switch}}, {{code|2=c|1=while}}, or {{code|2=c|1=do}}-{{code|2=c|1=while}} statements, and each of the three expressions in a <code>[[for loop#1972: C/C++|for]]</code> statement. #Before a function is entered in a function call. The order in which the arguments are evaluated is not specified, but this sequence point means that all of their side effects are complete before the function is entered. In the expression {{code|2=c|1=f(i++) + g(j++) + h(k++)}}, {{code|2=c|1=f}} is called with a parameter of the original value of {{code|2=c|1=i}}, but {{code|2=c|1=i}} is incremented before entering the body of {{code|2=c|1=f}}. Similarly, {{code|2=c|1=j}} and {{code|2=c|1=k}} are updated before entering {{code|2=c|1=g}} and {{code|2=c|1=h}} respectively. However, it is not specified in which order {{code|2=c|1=f()}}, {{code|2=c|1=g()}}, {{code|2=c|1=h()}} are executed, nor in which order {{code|2=c|1=i}}, {{code|2=c|1=j}}, {{code|2=c|1=k}} are incremented. If the body of {{code|2=c|1=f}} accesses the variables {{code|2=c|1=j}} and {{code|2=c|1=k}}, it might find both, neither, or just one of them to have been incremented. (The function call {{code|2=c|1=f(a,b,c)}} is ''not'' a use of the comma operator; the order of evaluation for {{code|2=c|1=a}}, {{code|2=c|1=b}}, and {{code|2=c|1=c}} is unspecified.) #At a function return, after the return value is copied into the calling context. (This sequence point is only specified in the C++ standard; it is present only implicitly in C.<ref>C++ standard, ISO 14882:2003, section 1.9, footnote 11.</ref>) #At the end of an [[initializer#C family of languages|initializer]]; for example, after the evaluation of {{code|2=c|1=5}} in the declaration {{code|2=c|1=int a = 5;}}. #Between each declarator in each declarator sequence; for example, between the two evaluations of {{code|2=c|1=a++}} in {{code|2=c|1=int x = a++, y = a++}}.<ref>C++ standard, ISO 14882:2003, section 8.3: "Each init-declarator in a declaration is analyzed separately as if it was in a declaration by itself."</ref> (This is ''not'' an example of the comma operator.) #After each conversion associated with an input/output format specifier. For example, in the expression {{code|2=c|1=printf("foo %n %d", &a, 42)}}, there is a sequence point after the {{code|1=%n}} is evaluated and before printing {{code|1=42}}. === C11 and C++11 === {{Section expand|date=April 2023}} 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, or the two can be "indeterminately" sequenced (one must complete before the other) or "unsequenced" (the operations in each expression may be interleaved). === C++17 === C++17 restricted several aspects of evaluation order. The {{code|new}} expression will always perform the memory allocation before evaluating the constructor arguments. The operators {{code|<<}}, {{code|>>}}, {{code|.}}, {{code|.*}}, {{code|->*}}, 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 <syntaxhighlight lang="cpp"> std::cout << a() << b() << c(); // parsed as (((std::cout << a()) << b()) << c()); </syntaxhighlight> is newly guaranteed to call {{code|a}}, {{code|b}} and {{code|c}} 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 {{code|a}} 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.<ref>{{cite web |last1=Dos Reis |first1=Gabriel |last2=Sutter |first2=Herb |last3=Caves |first3=Jonathan |title=Refining Expression Evaluation Order for Idiomatic C++ |url=https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0145r3.pdf |website=open-std.org |access-date=28 April 2023 |pages=1β5 |date=2016-06-23}}</ref>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)