title: virtual template function workaround
body: <p>I stumbled upon a problem I (nor do any of my more knowledgeable colleagues) know how to solve (of work around). The problem in the end comes to not being able to create a virtual template function. I have searched the net thoroughly, found several ways of dealing with it, but none seem to be applicable in my case.</p>
  
  <p>I don't know how to describe this situation in short, but I'll do my best and hope it makes sense.</p>
  
  <p>The problem is processing two curves, each of them consists of one or several segments of same or different curve types. So I began with creating an interface for a curve segment, with a template class of the curve it will be interacting with (functions are of the same type as intersections between two different curve segments, for example):</p>
  
  <pre><code>template&lt;class curve&gt; class curve_segment { // some methods here }
  </code></pre>
  
  <p>user can then implement several types of curves, and appropriate functions depending with what curves it will be interacting with. For example, <code>circle</code> and <code>line</code>, where both can interact with both:</p>
  
  <pre><code>class line :  public curve_segment&lt;line&gt;, public curve_segment&lt;circle&gt; { //... }
  class circle :  public curve_segment&lt;line&gt;, public curve_segment&lt;circle&gt; { //... }
  </code></pre>
  
  <p>Following that, I have a class <code>cell</code>, which depends on two curve segments, and a base class <code>cell_base</code> which encapsulates it:</p>
  
  <pre><code>template&lt;class curve1, class curve2&gt; class cell : public cell_base { 
    cell_base* up; cell_base* down;
  
    curve_segment&lt;curve1&gt;* segment_x;
    curve_segment&lt;curve2&gt;* segment_y;
  
    // some methods that depend on both curves
  }
  </code></pre>
  
  <p>In the end, there is a 2D grid m*n of such cells, where the two curves consist of m and n <code>curve_segments</code> of possibly different types, and is held together by the two pointers in each cell.</p>
  
  <p>The problem begins to show when a new curve_segment would be added to one of the two curves. The obvious solution would be to add </p>
  
  <pre><code>template&lt;class curve&gt; virtual void add_curve_x(curve_segment&lt;curve&gt; seg) =0;
  </code></pre>
  
  <p>to the <code>cell_base</code> class, where the implementation in <code>cell</code> could extract the appropriate segment of the other curve, and appended a new cell to it. For example, if curves <code>a</code> and <code>b</code> where <code>a</code> represents the x axis in the grid, and we were to add another curve segment to the curve <code>a</code>, we could find the right hand most cells of the grid, extract the curve on the y axis of each such cell, and from that and the newly provided segment make a new cell, which would append to it's right.</p>
  
  <p>Type erasure wouldn't work since we would need to know the type of the newly added segment at the point of the <code>cell</code> creation (we would need to provide types of both segments as template parameters to the <code>cell</code> class).</p>
  
  <p>Moving template to the <code>cell_base</code> class also wouldn't work, since that would mean that at the time of each <code>cell</code> allocation I would have to know the type of the next <code>curve_segment</code>.</p>
  
  <p>Is there a way around this?</p>
  
  <p><strong>edit</strong>:
  As suggested, adding the <code>add_curve_x</code> method:</p>
  
  <pre><code>template&lt;class curve1, class curve2&gt;
  template&lt;class curve&gt; 
  cell&lt;curve1, curve&gt;* cell&lt;curve1, curve2&gt;::add_curve_x(curve_segment&lt;curve1&gt;* seg) {
    return new cell&lt;curve1, curve&gt;(seg, (curve_segment&lt;curve&gt;*)(this-&gt;segment_y));
  }
  </code></pre>
  
  <p>In this case <code>seg</code> has to be of type <code>curve</code>, which has to implement <code>curve_segment&lt;curve1&gt;</code>. <code>segment_y</code> also has to implement <code>curve_segment&lt;curve&gt;</code>.</p>
  
  <p><strong>edit 2</strong>: explanation of why the cast is there</p>
  
  <p><a href="http://i48.tinypic.com/14y4kmv.png" rel="nofollow">pic</a></p>
  
  <p>Take the case with non-bolded rectangles. In that situations, there are two curves, curve <code>a</code> with 2 segments and curve <code>b</code> with 3 segments. class <code>line</code> has to implement <code>curve_segment&lt;line&gt;</code> as well as <code>curve_segment&lt;circle&gt;</code>, and circle has to implement both as well. Now let's add another bezier curve segment to curve <code>a</code>. class <code>bezier</code> has to implement <code>curve_segment&lt;line&gt;</code> and <code>curve_segment&lt;circle&gt;</code>, and <code>line</code> and <code>circle</code> have to implement <code>curve_segment&lt;bezier&gt;</code>. </p>
  
  <p>Let's look how we create the middle of the bolded three <code>cells</code>. We would make a call</p>
  
  <pre><code>add_curve_x&lt;bezier&gt;(new bezier(...));
  </code></pre>
  
  <p>on the cell object of type <code>cell&lt;circle, circle&gt;</code>.</p>
  
  <p>The argument <code>seg</code> will be a new <code>bezier</code> object (which can be cast to <code>curve_segment&lt;circle&gt;</code>), and that is also what <code>curve</code> type is, and <code>segment_y</code> will have to be cast to <code>curve_segment&lt;bezier&gt;</code>, since that's the curve type it's interacting with in the newly created cell.</p>