title: Bypass virtual template functions to achieve wished result
body: <p>My actual idea is not compiling due to C++ language limitation (templated virtual functions are not supported). Maybe some of you have some design recommendations for the following code snippet.</p>
  
  <p>I want to run different algorithms on different types of inputs. For example I have as input an integral image and an grayscale image. The integral image needs 32 bit for a pixel and my grayscale image needs 8 bit (just as an example). Therefore, I have two channels: <code>CChannel&lt;uint8&gt;</code> and <code>CChannel&lt;uint32&gt;</code></p>
  
  <p>Since I could have multiple channels for a single image, I store the channels in a vector, <code>std::vector&lt;CChannelBase*&gt;</code>... That's the justification of the class <code>CChannelBase</code>.</p>
  
  <pre class="lang-c prettyprint-override"><code>class CChannelBase
  {
  public:
      virtual ~CChannelBase( void ) = 0;
  };
  
  template &lt;class ValueType&gt;
  class CChannel : public CChannelBase
  {
  public:
      typedef ValueType value_type_t;
      Channel(): m_data_p(0) {}
      void setData(ValueType* f_data_p) { m_data_p = f_data_p; }
      const ValueType getData( void ) { return m_data_p; }
  private:
      ValueType* m_data_p;
  };
  </code></pre>
  
  <p>All my algorithms implement an interface and must be compatible with each image channel.</p>
  
  <pre class="lang-c prettyprint-override"><code>class IAlgorithmInterface
  {
  public:
      virtual ~IAlgorithmInterface() = 0;
      template &lt;class ValueType&gt;
      virtual void doWork(const CChannel&lt;ValueType&gt;* f_channel_p, float32_t&amp; f_result_r);
  };
  
  class CAlgorithmA : IAlgorithmInterface
  {
      CAlgorithmA() {...};
      ~CAlgorithmA() {...};
      template &lt;class ValueType&gt;
      void doWork(const CChannel&lt;ValueType&gt;* f_channel_p, float32_t&amp; f_result_r) {...};
  };
  
  class CAlgorithmB : IAlgorithmInterface
  {
      CAlgorithmB() {...};
      ~CAlgorithmB() {...};
      template &lt;class ValueType&gt;
      void doWork(const CChannel&lt;ValueType&gt;* f_channel_p, float32_t&amp; f_result_r) {...};
  };
  </code></pre>
  
  <p>Of course this code is not compiling since we have virtual template functions. Anyway, I am looking for a nice design to overcome this problem. A solution is that all classes (<code>IAlgorithmInterface</code>, <code>CAlgorithmA</code>, <code>CAlgorithmB</code>) are templatized which is a thing I do not want to do. I saw a few posts here, where the visitor pattern is recommended. But to be honest, I do not see how to use it in my case.</p>