title: Template friend function of a template class
body: <p>I was struggling with the issue described in <a href="https://stackoverflow.com/questions/1297609/overloading-friend-operator-for-template-class">this question</a> (declaring a template function as a friend of a template class), and I believe the 2nd answer is what I want to do (forward declare the template function, then name a specialization as a friend).  I have a question about whether a slightly different solution is actually correct or just happens to work in Visual C++ 2008.</p>
  
  <p>Test code is:</p>
  
  <pre><code>#include &lt;iostream&gt;
  
  // forward declarations
  template &lt;typename T&gt;
  class test;
  
  template &lt;typename T&gt;
  std::ostream&amp; operator&lt;&lt;(std::ostream &amp;out, const test&lt;T&gt; &amp;t);
  
  template &lt;typename T&gt;
  class test {
    friend std::ostream&amp; operator&lt;&lt; &lt;T&gt;(std::ostream &amp;out, const test&lt;T&gt; &amp;t);
    // alternative friend declaration
    // template &lt;typename U&gt;
    // friend std::ostream&amp; operator&lt;&lt;(std::ostream &amp;out, const test&lt;T&gt; &amp;t);
  
    // rest of class
    };
  
  template &lt;typename T&gt;
  std::ostream&amp; operator&lt;&lt;(std::ostream &amp;out, const test&lt;T&gt; &amp;t) {
    // output function defined here
    }
  </code></pre>
  
  <p>First, one strange thing I found was that if I change the forward declaration of <code>operator&lt;&lt;</code> so that it doesn't match (for example, <code>std::ostream&amp; operator&lt;&lt;(std::ostream &amp;out, int fake);</code>, everything still compiles and works correctly (to be clear, I don't need to define such a function, only declare it).  However, as in the linked-to question, removing the forward declaration causes a problem as the compiler seems to think I'm declaring a data member instead of a friend function.  I'm pretty sure that this behaviour is a Visual C++ 2008 bug.</p>
  
  <p>The interesting thing is when I remove the forward declarations and use the alternative friend declaration in the code above.  Note that the template parameter <code>U</code> doesn't appear in the following signature.  This method also compiles and works correctly (without changing anything else).  My question is whether this is conforming to the standard or an idiosyncrasy of Visual C++ 2008 (I couldn't find a good answer in my reference books).</p>
  
  <p>Note that while a friend declaration <code>template &lt;typename U&gt; friend ... const test&lt;U&gt; &amp;t);</code> also works, this actually gives each instance of the operator <code>friend</code> access to any instance of <code>test</code>, while what I want is that the private members of <code>test&lt;T&gt;</code> should only be accessible from <code>operator&lt;&lt; &lt;T&gt;</code>.  I tested this by instantiating a <code>test&lt;int&gt;</code> inside the <code>operator&lt;&lt;</code> and accessing a private member; this should cause a compile error when I try to output a <code>test&lt;double&gt;</code>.</p>
  
  <p>Synopsis:  Removing the forward declarations and switching to the alternative friend declaration in the code above seems to produce the same result (in Visual C++ 2008) -- is this code actually correct?</p>
  
  <p>UPDATE: Any of the above modifications to the code don't work under gcc, so I'm guessing that these are errors or "features" in the Visual C++ compiler.  Still I'd appreciate insights from people familiar with the standard.</p>