{"id":1144,"date":"2013-07-11T01:00:00","date_gmt":"2013-07-10T23:00:00","guid":{"rendered":"https:\/\/www.fussylogic.co.uk\/blog\/?p=1144"},"modified":"2013-07-10T12:37:43","modified_gmt":"2013-07-10T11:37:43","slug":"ordering","status":"publish","type":"post","link":"https:\/\/www.fussylogic.co.uk\/blog\/?p=1144","title":{"rendered":"Ordering"},"content":{"rendered":"<p>Consider this:<\/p>\n<pre class=\"sourceCode C\"><code class=\"sourceCode c\"><span class=\"ot\">#include &lt;iostream&gt;<\/span>\nusing namespace std;\n\n<span class=\"dt\">int<\/span> x = <span class=\"dv\">10<\/span>;\n\n<span class=\"dt\">int<\/span> f(<span class=\"dt\">int<\/span>&amp; x)\n{\n    x++;\n    <span class=\"kw\">return<\/span> x;\n}\n<span class=\"dt\">void<\/span> g(<span class=\"dt\">int<\/span>&amp; x)\n{\n    x *= <span class=\"dv\">2<\/span>;\n    <span class=\"kw\">return<\/span> x;\n}\n\n<span class=\"dt\">int<\/span> h(<span class=\"dt\">int<\/span> f, <span class=\"dt\">int<\/span> g)\n{\n    <span class=\"kw\">return<\/span> f + g;\n}\n\n<span class=\"dt\">int<\/span> main()\n{\n    clog &lt;&lt; <span class=\"st\">&quot;h() = &quot;<\/span> &lt;&lt; h(f(x), g(x)) &lt;&lt; endl;\n\n    <span class=\"kw\">return<\/span> <span class=\"dv\">0<\/span>;\n}<\/code><\/pre>\n<p>What would you say is the output?<\/p>\n<p>You might be surprised to learn that the answer is <em>undefined<\/em>.<\/p>\n<p>The call to <code>h()<\/code> is not required to evaluate its parameters in any particular order. Therefore <code>x<\/code> could be incremented first, or doubled first.<\/p>\n<ul>\n<li><code>f()<\/code> then <code>g()<\/code> means <code>h(11, 22)<\/code><\/li>\n<li><code>g()<\/code> then <code>f()<\/code> means <code>h(21, 20)<\/code><\/li>\n<\/ul>\n<p>Ouch.<\/p>\n<p>The reason this happens is because we\u00e2\u20ac\u2122ve violated a rule that goes right back to C. <em>You must not modify the same variable more than once between <a href=\"http:\/\/en.wikipedia.org\/wiki\/Sequence_point\">sequence points<\/a><\/em>. It\u00e2\u20ac\u2122s easy to forget that the arguments to a function are all within the same sequence point \u00e2\u20ac\u201c hence the comma between the parameters rather than a semi-colon.<\/p>\n<p>I mention it only because I read <a href=\"http:\/\/blog.regehr.org\/archives\/966\">this complaint<\/a>:<\/p>\n<blockquote>\n<p>In a sensible world, left-to-right evaluation would be specified by the standards.<\/p>\n<\/blockquote>\n<p>Unfortunately not. There is no reason that any particular ordering is \u00e2\u20ac\u0153correct\u00e2\u20ac\u009d, and C\/C++ has always been upfront about multiple access between two sequence points.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Consider this: #include &lt;iostream&gt; using namespace std; int x = 10; int f(int&amp; x) { x++; return x; } void g(int&amp; x) { x *= 2; return x; } int h(int f, int g) { return f + g; } int main() { clog &lt;&lt; &quot;h() = &quot; &lt;&lt; h(f(x), g(x)) &lt;&lt; endl; return 0;\u2026 <span class=\"read-more\"><a href=\"https:\/\/www.fussylogic.co.uk\/blog\/?p=1144\">Read More &raquo;<\/a><\/span><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[66,6],"_links":{"self":[{"href":"https:\/\/www.fussylogic.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1144"}],"collection":[{"href":"https:\/\/www.fussylogic.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.fussylogic.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.fussylogic.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.fussylogic.co.uk\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1144"}],"version-history":[{"count":2,"href":"https:\/\/www.fussylogic.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1144\/revisions"}],"predecessor-version":[{"id":1146,"href":"https:\/\/www.fussylogic.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1144\/revisions\/1146"}],"wp:attachment":[{"href":"https:\/\/www.fussylogic.co.uk\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1144"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.fussylogic.co.uk\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1144"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.fussylogic.co.uk\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1144"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}