× {{alert.msg}} Never ask again
Get notified about new tutorials RECEIVE NEW TUTORIALS

operator<< chaining and function call order

Arnaud Bellec
Jun 12, 2015
<p><code>operator&lt;&lt;</code> associates left-to-right, which means that the first statement</p> <pre><code>std::cout &lt;&lt; std::hex &lt;&lt; centered(a,cout) &lt;&lt; std::dec; </code></pre> <p>is equivalent to </p> <pre><code>operator&lt;&lt;( operator&lt;&lt;( operator&lt;&lt;( std::cout, std::hex ), centered(a, cout) ), std::dec ); </code></pre> <p>With this syntax, it appears that <code>operator&lt;&lt;(std::cout, std::hex)</code> and <code>centered(a, cout)</code> are both parameters to <code>operator&lt;&lt;</code>. Hence, as per the standard, their order is unspecified: It's not Undefined Behavior, but the compiler is free to choose the order it prefers.</p> <p>This tip was originally posted on <a href="http://stackoverflow.com/questions/26910675/operator&lt;&lt;%20chaining%20and%20function%20call%20order/26911212">Stack Overflow</a>.</p>

Get New Tutorials Delivered to Your Inbox

New tutorials will be sent to your Inbox once a week.

comments powered by Disqus