How does operator chaining happen in C++?
<p>I have a totally basic C++ question here.</p>

<pre><code>#include &lt;iostream&gt;
using namespace std;

int main() {
    int a = 255;
    cout &lt;&lt; hex &lt;&lt; a &lt;&lt; endl; // &lt;-----
}
</code></pre>

<p><strong>In the code piece above, how is the <code>std::cout</code> statement chained?</strong></p>

<p>I understand that an implementation of <code>cout</code> would return the reference to <code>cout</code> object to allow chaining to happen, so it should be executed as:</p>

<pre><code>(((cout &lt;&lt; hex) &lt;&lt; a) &lt;&lt; endl)
</code></pre>

<p>i.e. equivalent to these, in order</p>

<ol>
<li><code>cout &lt;&lt; hex</code></li>
<li><code>cout &lt;&lt; a</code></li>
<li><code>cout &lt;&lt; endl</code></li>
</ol>

<p>But this cannot be the case because somehow value of <code>a</code> needs to be converted to <code>hex</code> form! </p>

<p><strong>How are operators actually chained by the compiler to make the conversion happen?</strong></p>
