Templates and friends
<p>While reading from Thinking in C++ Volume 2, from the Templates in Depth chapter, I see that if you have a friendly function in a template class, you need to forward declaration that function. I made this example to test this, overlaoding the output operator:</p>

<pre><code>#include &lt;iostream&gt;
using namespace std;
/*
template&lt;class T&gt;
class X;
template&lt;class T&gt;
ostream&amp; operator &lt;&lt; (ostream&amp; os, const X&lt;T&gt;&amp; x);
*/
template&lt;class T&gt;
class X {
    T t;
public:
    X(T i): t(i) {}
    friend ostream&amp; operator &lt;&lt; (ostream&amp; os, const X&lt;T&gt;&amp; x)
        {
            return os &lt;&lt; x.t;
        }
};


int main()
{
    X&lt;int&gt; a(1);
    cout &lt;&lt; a;
    return 0;
}
</code></pre>

<p>But it works without the forward declaration, but then I test it with the definition of &lt;&lt; outside of the class:</p>

<pre><code>friend ostream&amp; operator &lt;&lt; &lt;&gt;(ostream&amp; os, const X&lt;T&gt;&amp; x); (inside of the class)

template&lt;class T&gt;
ostream&amp; operator &lt;&lt; (ostream&amp; os, const X&lt;T&gt;&amp; x)
{
        return os &lt;&lt; x.t;
}
</code></pre>

<p>I am not sure why with the definition inside of the class it doesnt apply, is it because you have to explicit say that the ostream operator function is a template ? (using &lt;> ) ??   </p>

<p>Sorry for the confusion.</p>
