title: C++ derived class is templated, base is not: how to get type from base?
body: <p>I'm using a 3rd party proprietary software package. It uses a data model that looks like this:</p>
  
  <pre><code>class Base {
      ...
  }
  
  template&lt;class T&gt;
  class Derived: public Base {
  protected:
    T _t;
  public:
  T&amp; getData();
  }
  </code></pre>
  
  <p>When I interact with their code, they hand me pointers to <code>Base</code> objects. I'd like to write some templated functions of my own. How can I do this? i.e. if I knew the type T, I could cast it, but what if I don't know the type? What I'd like is a function looking something like this:</p>
  
  <pre><code>template&lt;T&gt;
  DataToString(Derived&lt;T&gt; d){
      std::stringstream ss;
      ss &lt;&lt; d.getData();
      return ss.str();
  }
  </code></pre>
  
  <p>which may be called:
      Base b;
      std::cout &lt;&lt; DataToString(b);</p>
  
  <p>but when I try that, the compiler tells me it can't match the templates. What I've got right now is a "guess and check" if/else block for each data type and I"m wondering if there's a better way.</p>
  
  <p>I think my question sort of related to <a href="https://stackoverflow.com/questions/9560861/downcasting-from-base-pointer-to-templated-derived-types">this</a>, but in my case I'm using a 3rd party library.</p>