Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Yes.</p> <p>Built-in logical AND (<code>&amp;&amp;</code>), logical OR (<code>||</code>) and the comma operator (<code>,</code>) are the <strong>only</strong> cases in which for a binary operator C++ guarantees that evaluation will compute left expression and then (if not short circuited) right expression (comma operator of course always evaluates both operands, first left and then right).</p> <p>Note also that the comma between function arguments is not a comma operator and therefore the order of evaluation of function arguments is not specified and even worse than that: for example in <code>f(g(h()),i())</code> it is possible that the sequence of calls will be <code>h,i,g,f</code>.</p> <p>Also the guarantee about evaluation order only applies to built-in operators; if you redefine them then they basically become function calls where the order of evaluation of arguments is not guaranteed and where short-circuiting is not performed.</p> <p>Other binary operators don't guarantee the order of evaluation and for example one common mistake is to think that in:</p> <pre><code>std::cout &lt;&lt; foo() &lt;&lt; bar(); </code></pre> <p>the call to <code>foo()</code> is guaranteed to happen before the call to <code>bar()</code> ... this is <strong>not</strong> true.</p> <p>Of course the order of evaluation is also guaranteed for the <strong>ternary</strong> <code>:?</code> operator, where only one of the two other expressions will be evaluated after first evaluating the condition.</p> <p>Another place in which the order of evaluation is guaranteed (and sometimes surprising for newbies) is member initialization lists for constructors, but in this case the order is <strong>not</strong> the one in the expression, but the order of member declaration in the class.... for example:</p> <pre><code>struct Foo { int x, y; Foo() : y(compute_y()), x(compute_x()) {} }; </code></pre> <p>in this case it is <strong>guaranteed</strong> that the call <code>compute_x()</code> will be done BEFORE the call <code>compute_y()</code> because <code>x</code> precedes <code>y</code> in the member declarations.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload