title: Container of pointers to derived class from template class with common ancestor as template argument
body: <p>I have a super class that models a <code>Measurement</code> and two derived classes: <code>PointCloudMeasurement</code> and <code>ImageMeasurement</code>. </p>
  
  <p>Another templated super class, <code>Handler&lt;MeasT&gt;</code> defines the interface to process the measurement.</p>
  
  <p>Two handler classes, <code>PointCloudHandler</code> and <code>ImageHandler</code> specialize the <code>Handler</code> class with the corresponding measurement types as template arguments. </p>
  
  <p>I would like to keep track of all the handlers using a container of pointers to the super class, and call the appropriate method depending on the derive type of handler at hand.</p>
  
  <p>Here is the code (<a href="http://cpp.sh/5zgkh" rel="nofollow noreferrer">link</a>):</p>
  
  <pre class="lang-cpp prettyprint-override"><code>// Example program
  #include &lt;iostream&gt;
  #include &lt;string&gt;
  #include &lt;vector&gt;
  #include &lt;memory&gt;
  
  class Measurement {
  public:
    uint64_t time;
    virtual ~Measurement() {}
  };
  
  class PointCloudMeasurement : public Measurement {
  public:
    uint64_t n_points = 64*1024;
  
  };
  
  class ImageMeasurement : public Measurement {
  public:
    uint64_t n_pixels = 640*480;
  
  };
  
  template&lt;class MeasT = Measurement&gt;
  class Handler {
  public:
    virtual void addMeasurement(MeasT&amp; m) = 0;
  };
  
  class PointCloudHandler : public Handler&lt;PointCloudMeasurement&gt; {
  public:
    void addMeasurement(PointCloudMeasurement&amp; m) override {
      std::cout &lt;&lt; m.n_points &lt;&lt; std::endl;
    }
  };
  
  class ImageHandler : public Handler&lt;ImageMeasurement&gt; {
    void addMeasurement(ImageMeasurement&amp; m) override {
      std::cout &lt;&lt; m.n_pixels &lt;&lt; std::endl;
  
    }
  
  };
  
  
  int main(void){
    std::vector&lt;std::unique_ptr&lt;Handler&lt;Measurement&gt;&gt;&gt; handlers;
  
    handlers.push_back(std::make_unique&lt;PointCloudHandler&gt;());
    handlers.push_back(std::make_unique&lt;ImageHandler&gt;());
  
    PointCloudMeasurement pcm;
    ImageMeasurement im;
  
    handlers[0]-&gt;addMeasurement(pcm);
    handlers[1]-&gt;addMeasurement(im);
  }
  </code></pre>
  
  <p>which gives me:</p>
  
  <pre class="lang-cpp prettyprint-override"><code>In function 'int main()':
  50:59: error: no matching function for call to 'std::vector&lt;std::unique_ptr&lt;Handler&lt;Measurement&gt; &gt; &gt;::push_back(std::_MakeUniq&lt;PointCloudHandler&gt;::__single_object)'
  50:59: note: candidates are:
  In file included from /usr/include/c++/4.9/vector:64:0,
                   from 4:
  /usr/include/c++/4.9/bits/stl_vector.h:913:7: note: void std::vector&lt;_Tp, _Alloc&gt;::push_back(const value_type&amp;) [with _Tp = std::unique_ptr&lt;Handler&lt;Measurement&gt; &gt;; _Alloc = std::allocator&lt;std::unique_ptr&lt;Handler&lt;Measurement&gt; &gt; &gt;; std::vector&lt;_Tp, _Alloc&gt;::value_type = std::unique_ptr&lt;Handler&lt;Measurement&gt; &gt;]
         push_back(const value_type&amp; __x)
         ^
  /usr/include/c++/4.9/bits/stl_vector.h:913:7: note:   no known conversion for argument 1 from 'std::_MakeUniq&lt;PointCloudHandler&gt;::__single_object {aka std::unique_ptr&lt;PointCloudHandler, std::default_delete&lt;PointCloudHandler&gt; &gt;}' to 'const value_type&amp; {aka const std::unique_ptr&lt;Handler&lt;Measurement&gt; &gt;&amp;}'
  /usr/include/c++/4.9/bits/stl_vector.h:931:7: note: void std::vector&lt;_Tp, _Alloc&gt;::push_back(std::vector&lt;_Tp, _Alloc&gt;::value_type&amp;&amp;) [with _Tp = std::unique_ptr&lt;Handler&lt;Measurement&gt; &gt;; _Alloc = std::allocator&lt;std::unique_ptr&lt;Handler&lt;Measurement&gt; &gt; &gt;; std::vector&lt;_Tp, _Alloc&gt;::value_type = std::unique_ptr&lt;Handler&lt;Measurement&gt; &gt;]
         push_back(value_type&amp;&amp; __x)
         ^
  /usr/include/c++/4.9/bits/stl_vector.h:931:7: note:   no known conversion for argument 1 from 'std::_MakeUniq&lt;PointCloudHandler&gt;::__single_object {aka std::unique_ptr&lt;PointCloudHandler, std::default_delete&lt;PointCloudHandler&gt; &gt;}' to 'std::vector&lt;std::unique_ptr&lt;Handler&lt;Measurement&gt; &gt; &gt;::value_type&amp;&amp; {aka std::unique_ptr&lt;Handler&lt;Measurement&gt; &gt;&amp;&amp;}'
  51:54: error: no matching function for call to 'std::vector&lt;std::unique_ptr&lt;Handler&lt;Measurement&gt; &gt; &gt;::push_back(std::_MakeUniq&lt;ImageHandler&gt;::__single_object)'
  51:54: note: candidates are:
  In file included from /usr/include/c++/4.9/vector:64:0,
                   from 4:
  /usr/include/c++/4.9/bits/stl_vector.h:913:7: note: void std::vector&lt;_Tp, _Alloc&gt;::push_back(const value_type&amp;) [with _Tp = std::unique_ptr&lt;Handler&lt;Measurement&gt; &gt;; _Alloc = std::allocator&lt;std::unique_ptr&lt;Handler&lt;Measurement&gt; &gt; &gt;; std::vector&lt;_Tp, _Alloc&gt;::value_type = std::unique_ptr&lt;Handler&lt;Measurement&gt; &gt;]
         push_back(const value_type&amp; __x)
         ^
  /usr/include/c++/4.9/bits/stl_vector.h:913:7: note:   no known conversion for argument 1 from 'std::_MakeUniq&lt;ImageHandler&gt;::__single_object {aka std::unique_ptr&lt;ImageHandler, std::default_delete&lt;ImageHandler&gt; &gt;}' to 'const value_type&amp; {aka const std::unique_ptr&lt;Handler&lt;Measurement&gt; &gt;&amp;}'
  /usr/include/c++/4.9/bits/stl_vector.h:931:7: note: void std::vector&lt;_Tp, _Alloc&gt;::push_back(std::vector&lt;_Tp, _Alloc&gt;::value_type&amp;&amp;) [with _Tp = std::unique_ptr&lt;Handler&lt;Measurement&gt; &gt;; _Alloc = std::allocator&lt;std::unique_ptr&lt;Handler&lt;Measurement&gt; &gt; &gt;; std::vector&lt;_Tp, _Alloc&gt;::value_type = std::unique_ptr&lt;Handler&lt;Measurement&gt; &gt;]
         push_back(value_type&amp;&amp; __x)
         ^
  /usr/include/c++/4.9/bits/stl_vector.h:931:7: note:   no known conversion for argument 1 from 'std::_MakeUniq&lt;ImageHandler&gt;::__single_object {aka std::unique_ptr&lt;ImageHandler, std::default_delete&lt;ImageHandler&gt; &gt;}' to 'std::vector&lt;std::unique_ptr&lt;Handler&lt;Measurement&gt; &gt; &gt;::value_type&amp;&amp; {aka std::unique_ptr&lt;Handler&lt;Measurement&gt; &gt;&amp;&amp;}'
  </code></pre>
  
  <p>I don't understand why the pointer conversion does not work, since all the derived classes inherit from a <code>Handler&lt;MeasT&gt;</code> where <code>MeasT</code> is a derivation of <code>Measurement</code>.</p>
  
  <p>I've seen similar questions but most of them keep the template even in the derived class (which I don't) or refer to the CRTP. However, the CRTP uses directly the derived class as template argument, while here I'm using a different derived class.</p>