title: C++ : alternative solution to a template
body: <p>There is a lightweight class that plays the central role in a code. Actually, there are two variants (say, <code>V0</code> and <code>V1</code>); both have method <code>do_it()</code>. This method takes about 10 floating point operations (FPOs) in <code>V0</code> and about 20 FPOs in <code>V1</code>. This is a small number but <code>do_it()</code> is being called all the time and overall these calls take 30% - 70% of execution time.</p>
  
  <p>In my current implementation <code>V0</code> and <code>V1</code> are template parameters for all other classes that depend on them so the correct variant of <code>do_it()</code> is decided at compilation.</p>
  
  <p>The problem is basically <em>any</em> class in the code uses <code>V</code> or uses a class that uses <code>V</code>, so in practice all classes are templates. Over time it becomes a bit annoying and hard to maintain.</p>
  
  <p>What other mechanism of C++ can I use instead of the templates? Can anyone estimate an overhead for this case? Of course I will learn the actual numbers when I rewrite the code, but this is quite an investment</p>
  
  <p><strong>EDIT</strong>
  Let me also provide a (very simplified) example:</p>
  
  <p>Let's say <code>V0</code> and <code>V1</code> are points in 2D space. <code>V0</code> is on a plane and <code>V1</code> on a sphere.
  <code>do_it()</code> is a method that calculates the distance between points and in the case of <code>V0</code> is much faster than for <code>V1</code></p>
  
  <p>Then I have classes e.g. <code>Points&lt;V&gt;</code> which inherits from a <code>std::vector&lt;V&gt;</code>, <code>RandomWalk&lt;V&gt;</code> that keeps a reference to <code>Points&lt;V&gt;</code>, lots of analysis code like <code>plot_distribution&lt;V&gt;(Points&lt;V&gt;)</code> etc.</p>
  
  <p><em>Maybe</em> at some point in the future a user will be allowed to provide their own geometries e.g. a torus. But as for now there are only two geometries allowed and they never appear in the program both at the same time.</p>