Do I really need to bend over backwards for a friend operator<< for a class in a namespace?
<p>I want to implemnent an operator&lt;&lt;  for streaming my class (say it's named <code>Paragraph</code>). Class <code>Paragraph</code> has some private data, for which reason I want the (freestanding) operator&lt;&lt; function to be a friend. So, I do as suggested, e.g., <a href="https://stackoverflow.com/a/237074/1593077">here on SO</a>. <code>friend</code> statement, implement the <code>operator&lt;&lt;</code> and all is well.</p>

<p>But now I want to put Paragraph inside a namespace, say <code>namespace foo</code>. It no longer works! If I write:</p>

<pre><code>namespace foo {
class Paragraph {
    public:
        explicit Paragraph(std::string const&amp; init) :m_para(init) {}
        std::string const&amp;  to_str() const { return m_para; }
    private:
        friend std::ostream &amp; operator&lt;&lt;(std::ostream &amp;os, const Paragraph&amp; p);
        std::string     m_para;
};
} // namespace foo
</code></pre>

<p>The compiler tells me I've befriended <code>foo::operator&lt;&lt;</code>, not <code>::operator&lt;&lt;</code>. Ok, fair enough. So, I replace the friend line with:</p>

<pre><code>    friend std::ostream &amp; ::operator&lt;&lt;(std::ostream &amp;os, const Paragraph&amp; p);
</code></pre>

<p>but I get an error again (from GCC 5.4.0), telling me the <code>::operator&lt;&lt;</code> has not having been declared. Ok, let's declare it then. Will this work?:</p>

<pre><code>namespace foo {
std::ostream &amp; ::operator&lt;&lt;(std::ostream &amp;os, const foo::Paragraph&amp; p);
class Paragraph {
    public:
        explicit Paragraph(std::string const&amp; init) :m_para(init) {}
        std::string const&amp;  to_str() const { return m_para; }
    private:
        friend std::ostream &amp; operator&lt;&lt;(std::ostream &amp;os, const Paragraph&amp; p);
        std::string     m_para;
};
} // namespace foo
</code></pre>

<p>Nope, it doesn't know about Paragraph when reading the declaration of <code>::operator&lt;</code>. Ok, let's forward-declare:</p>

<pre><code>namespace foo {
class Paragraph;
std::ostream &amp; ::operator&lt;&lt;(std::ostream &amp;os, const foo::Paragraph&amp; p);
class Paragraph { /* actual definition here */ } }
std::ostream &amp; operator&lt;&lt;(std::ostream &amp;os, const foo::Paragraph&amp; p) { /* impl */ }
</code></pre>

<p>no luck. I get the weird error:</p>

<pre><code>f.cpp:23:16: note: candidate: std::ostream&amp; operator&lt;&lt;(std::ostream&amp;, const foo::Paragraph&amp;)
 std::ostream &amp; operator&lt;&lt;(std::ostream &amp;os, const foo::Paragraph&amp; p)
                ^
f.cpp:11:15: note: candidate: std::ostream&amp; operator&lt;&lt;(std::ostream&amp;, const foo::Paragraph&amp;)
 std::ostream&amp; ::operator&lt;&lt;(std::ostream &amp;os, const foo::Paragraph&amp; p);
</code></pre>

<p>... and here I was thinking the global namespace and the :: namespace are the same thing... arent' they? (sigh). No matter, let's move the declaration out of the namespace:</p>

<pre><code>class foo::Paragraph;
std::ostream &amp; operator&lt;&lt;(std::ostream &amp;os, const foo::Paragraph&amp; p);
namespace foo { class Paragraph { /* actual definition here */ } }
std::ostream &amp; operator&lt;&lt;(std::ostream &amp;os, const foo::Paragraph&amp; p) { /* impl */ }
</code></pre>

<p>Still not good enough, now I get the "'foo' has not been declared" error. (granshes teeth) fine! Be that way!</p>

<pre><code>namespace foo { class Paragraph; }
std::ostream &amp; operator&lt;&lt;(std::ostream &amp;os, const foo::Paragraph&amp; p);

namespace foo { class Paragraph { /* actual definition here */ } }
std::ostream &amp; operator&lt;&lt;(std::ostream &amp;os, const foo::Paragraph&amp; p) { /* impl */ }
</code></pre>

<p>so this, but no less than this, works. That's <em>terrible</em>! Surely there must be some kind of less verbose way to do it... right?</p>

<p><strong>Note:</strong> Assume the <code>operator&lt;&lt;</code> can't be inlined and must be defined separately.</p>