title: Operator overloading and namespaces
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/3623631/where-should-non-member-operator-overloads-be-placed">Where should non-member operator overloads be placed?</a>  </p>
  </blockquote>
  
  
  
  <p>While browsing on SO, I often find questions or answer that involves overloading/defining a <code>std::ostream&amp; operator&lt;&lt;(std::ostream&amp; os, const Foo&amp; foo)</code> or a <code>Foo operator+(const Foo&amp; l, const Foo&amp; r)</code>.</p>
  
  <p>While I know how and when (not) to write these operators, I'm confused about the <code>namespace</code> thing.</p>
  
  <p>If I have the following class:</p>
  
  <pre><code>namespace bar
  {
    class Foo {};
  }
  </code></pre>
  
  <p>In which <code>namespace</code> should I write the different operator definitions ?</p>
  
  <pre><code>// Should it be this
  
  namespace bar
  {
    std::ostream&amp; operator&lt;&lt;(std::ostream&amp; os, const Foo&amp; foo);
  }
  
  // Or this ?
  
  namespace std
  {
    ostream&amp; operator&lt;&lt;(ostream&amp; os, const bar::Foo&amp; foo);
  }
  
  // Or this ?
  
  std::ostream&amp; operator&lt;&lt;(std::ostream&amp; os, const bar::Foo&amp; foo);
  </code></pre>
  
  <p>The same question applies for the <code>operator+</code>. So, what is the good practice here and <strong>why</strong> ?</p>