title: How to have many namespaces while keeping a 'clean' api? namespace composition
body: <p><strong>EDIT</strong>: This does not really seem a duplicate to me since <a href="https://stackoverflow.com/questions/41590/how-do-you-properly-use-namespaces-in-c">this question</a> in broader in scope (<em>how to use it</em>). Anyways, the answer to my question does turn out to be there in paercebal's <a href="https://stackoverflow.com/a/81602/1248364">answer</a> and is <strong>namespace composition</strong>. Another related source in SO is this other <a href="https://stackoverflow.com/questions/16871390/why-is-namespace-composition-so-rarely-used">question</a>.</p>
  
  <hr>
  
  <p>I've been trying to modularize my code using namespaces. Anon's <a href="https://stackoverflow.com/a/713717/1248364">answer</a> to a question on nested namespaces got me wondering whether they should be used for design instead of simply avoiding name clashes.</p>
  
  <p>I feel like namespaces (occasionally nested ones) are useful to organize the code, but sure things like <code>world::europe::spain::madrid</code> become cumbersome to use. Therefore, from an API point of view, having most code in the same namespace (just like the <code>std</code> library) works best.</p>
  
  <p>My question is then two fold:</p>
  
  <ol>
  <li><p><strong>Should namespaces be used to organize code or simply to avoid
  name clashes?</strong> and if so</p></li>
  <li><p><strong>How to have a "complex" namespace structure while keeping the API
  clean?</strong></p></li>
  </ol>
  
  <p>Regarding question 2, I've been using the following strategy:</p>
  
  <p>(1) having some code with its own namespace</p>
  
  <pre><code>//mathlib.h
  
  namespace mathlib {
      int sum( int a, int b ) {
          return a + b;
      }
  }
  </code></pre>
  
  <p>(2) and bring it into another namespace</p>
  
  <pre><code>//otherlib.h
  
  #include &lt;iostream&gt;
  #include "mathlib.h"
  
  namespace otherlib {
      using namespace mathlib; // lookup stuff inside mathlib
  
      void print(int n) {
          std::cout &lt;&lt; "the int is " &lt;&lt; n &lt;&lt; "!";
      }
  }
  </code></pre>
  
  <p>(3) So I can actually call <code>sum</code> as it were under the <code>otherlib</code> namespace.</p>
  
  <pre><code>#include "otherlib.h"
  
  int main(){
      auto myint = otherlib::sum(1, 2);
      otherlib::print(myint);
      return 0;
  }
  </code></pre>