title: C++ Vector of templated class objects
body: <p>I don't know if it's possible to achieve what I'm trying but maybe someone can guide me in the right direction. <em>Sidenote: this is on an embedded system.</em></p>
  
  <p>I have an SPI communication that returns a <code>std::vector&lt;int&gt;</code> representing a set of registers. This is done in a separate thread so asynch.
  To handle this, I create an object on every <code>read</code> call containing a <code>std::promise&lt;std::vector&lt;int&gt;&gt;</code> (and some other information) and directly return the <code>future</code> of it, so the caller can wait for the result.</p>
  
  <p>Once the SPI transfer is complete, I set the value of the promise accordingly. So far so good.</p>
  
  <p>Now what I'd like to do is to not always return a future of type <code>std::vector&lt;int&gt;</code> but do some preprocessing on the vector and return either <code>int</code> or <code>double</code> or even a <code>struct</code>.
  I thought I can achieve that by handing an anonymous function to the <code>read</code> function and store a reference in the object. This function would then be called before the promise is assigned.</p>
  
  <p>The problem I now have is that the return type of this function directly affects the return type of my promise and the object storing them is not always the same anymore. So I thought I just template it</p>
  
  <pre><code>template &lt;class ret_type&gt;
  class obj{
      public:
      std::promise&lt;ret_type&gt; prms;
      ret_type (*map_function)(std::vector&lt;int&gt;);
      ...
  };
  </code></pre>
  
  <p>But now I have to problem that my vector containing these objects (the SPI queue) that was of type <code>std::vector&lt;obj&gt;</code> cannot hold objects of different types.</p>
  
  <p>Is there a proper way to solve that?</p>
  
  <p><strong>edit:</strong>
  I'd use the vector in a way like</p>
  
  <pre><code>for(auto &amp;element : vector){
      std::vector&lt;int&gt; answer_from_spi = spi_read();
      element.prms.set_value(element.map_function(answer_from_spi));
  }
  </code></pre>