title: C++ Namespaces & templates
body: <p>I have some functions that can be grouped together, but don't belong to some object / entity and therefore can't be treated as methods. </p>
  
  <p>So, basically in this situation I would create a new namespace and put the definitions in a <code>header</code> file, the implementation in <code>cpp</code> file. Also (if needed) I would create an anonymous namespace in that <code>cpp</code> file and put all additional functions that don't have to be exposed / included to my namespace's interface there.</p>
  
  <p>See the code below (probably not the best example and could be done better with another program architecture, but I just can't think of a better sample...)</p>
  
  <p><em>Sample code (<code>header</code>)</em></p>
  
  <pre><code>namespace algorithm {
     void HandleCollision(Object* object1, Object* object2);
  }
  </code></pre>
  
  <p><em>Sample code (<code>cpp</code>)</em></p>
  
  <pre><code>#include "header"
  
  // Anonymous namespace that wraps 
  // routines that are used inside 'algorithm' methods
  // but don't have to be exposed
  namespace {
     void RefractObject(Object* object1) {
        // Do something with that object
        // (...)
     }
  }
  
  namespace algorithm {
     void HandleCollision(Object* object1, Object* object2) {
        if (...) RefractObject(object1);
     }
  }
  </code></pre>
  
  <p><strong>So far so good.</strong> I guess this is a good way to manage my code, but I don't know what should I do if I have some template-based functions and want to do basically the same.</p>
  
  <p>If I'm using templates, I have to put all my code in the <code>header</code> file. Ok, but how should I conceal some implementation details then? </p>
  
  <p>I want to hide <code>RefractObject</code> function from my interface, but I can't simply remove its declaration (just because I have all my code in a <code>header</code> file)...</p>
  
  <p>The only approach I came up with was something like:</p>
  
  <p><em>Sample code (<code>header</code>)</em></p>
  
  <pre><code>namespace algorithm {
     // Is still exposed as a part of interface!
     namespace impl {
        template &lt;typename T&gt;
        void RefractObject(T* object1) {
           // Do something with that object
           // (...)
        }
     }
  
     template &lt;typename T, typename Y&gt;
     void HandleCollision(T* object1, Y* object2) {
        impl::RefractObject(object1);
        // Another stuff
     }
  }
  </code></pre>
  
  <p>Any ideas how to make this better in terms of code designing?</p>