title: How to simulate virtuality for method template
body: <p>I have a class hierarchy where I want to introduce a method template that would behave like if it was virtual. For example a simple hierarchy:</p>
  
  <pre><code>class A {
    virtual ~A() {}
  
    template&lt;typename T&gt;
    void method(T &amp;t) {}
  };
  
  class B : public A {
    template&lt;typename T&gt;
    void method(T &amp;t) {}
  };
  </code></pre>
  
  <p>Then I create object B:</p>
  
  <pre><code>A *a = new B();
  </code></pre>
  
  <p>I know I can get the type stored in <code>a</code> by <code>typeid(a)</code>. How can I call the correct <code>B::method</code> dynamically when I know the type? I could probably have a condition like: </p>
  
  <pre><code>if(typeid(*a)==typeid(B))
      static_cast&lt;B*&gt;(a)-&gt;method(params);
  </code></pre>
  
  <p>But I would like to avoid having conditions like that. I was thinking about creating a <code>std::map</code> with <code>typeid</code> as a key, but what would I put as a value?</p>