title: Polymorphism in generic classes not working in C++0x
body: <p>Basically from C++ FAQ I learned that:</p>
  
  <blockquote>
    <p>A virtual function allows derived classes to replace the
    implementation provided by the base class. The compiler makes sure the
    replacement is always called whenever the object in question is
    actually of the derived class, even if the object is accessed by a
    base pointer rather than a derived pointer. This allows algorithms in
    the base class to be replaced in the derived class, even if users
    don't know about the derived class.</p>
  </blockquote>
  
  <p>Though, using templates this becomes no more correct. </p>
  
  <p>I need to have a template class which is derived by another template class. After that I found it makes no sense to have a virtual function in a base template class, googling around I found that there is a technique to overcome limitations of polymorphism in generic programming called Type Erasure. </p>
  
  <p>Examples and tutorials I've read, are almost irrelevant to my needs. Also I'm not sure if I understood concepts of Type Erasure correctly. Being a beginner, I need to simply do:</p>
  
  <pre><code>template &lt;typename T&gt;
  class Foo
  {
  public:
      virtual void doX(){cout&lt;&lt;"doX from Foo"&lt;&lt;endl;}
  };
  
  template &lt;typename T&gt;
  class Bar : public Foo&lt;T&gt;
  {
  public:
      void doX(){cout&lt;&lt;"doX from Bar"&lt;&lt;endl;}
  };
  </code></pre>
  
  <p>And,</p>
  
  <pre><code>Foo&lt;int&gt;* p;
  Foo&lt;int&gt; i;
  i.doX();           // "doX from Foo", it's ok
  Bar&lt;int&gt; j;
  j.doX();           // "doX from Bar", it's ok
  p=&amp;i;
  p-&gt;doX();          // should be "doX from Foo", it's ok
  p=&amp;j;
  p-&gt;doX();          // I expect "doX from Bar", but it's "doX from Foo"
  </code></pre>
  
  <p><strong>EDIT:</strong>
  My question is how to implement above behavior? Without templates, It works as expected. I need same for general classes. </p>
  
  <p>Simply, I have a base class <code>B</code>. It's a template class. It has a virtual function. And also I have a subclass of <code>B</code> which is a template class too, and it should reimplement some behavior of  <code>B</code> (somehow implement virtual function of <code>B</code>).</p>
  
  <p><strong>EDIT2 : Just edited the title.</strong>
  I was compiling my code with g++ 4.6.3 compiler using flag <code>-std=c++0x</code> because I need <code>std::thread</code> and <code>std::unique_ptr</code> in my code. Surprisingly, removing the <code>-std=c++0x</code> the above code works as expected. I also tried g++ 4.7 and there was no problem with both 2011 and 2003 standards. </p>