title: C++ Techniques: Type-Erasure vs. Pure Polymorphism
body: <p>What are the advantages/disadvantages of the two techniques in comparison ? And more importantly: Why and when should one be used over the other ? Is it just a matter of personal taste/preference ?</p>
  
  <p>To the best of my abilities, I haven't found another post that explicitly addresses my question. Among many questions regarding the actual use of polymorphism and/or type-erasure, the following seems to be closest, or so it seemed, but it doesn't really address my question either:</p>
  
  <p><a href="https://stackoverflow.com/questions/4316317/c-crtp-type-erasure-vs-polymorphism">C++ -&amp; CRTP . Type erasure vs polymorphism</a></p>
  
  <p>Please, note that I very well understand both techniques. To this end, I provide a simple, self-contained, working example below, which I'm happy to remove, if it is felt unnecessary. However, the example should clarify what the two techniques mean with respect to my question. I'm not interested in discussing nomenclatures. Also, I know the difference between compile- and run-time polymorphism, though I wouldn't consider this to be relevant to the question. Note that my interest is less in performance-differences, if there are any. However, if there was a striking argument for one or the other based on performance, I'd be curious to read it. In particular, I would like to hear about concrete examples (no code) that would really only work with one of the two approaches.</p>
  
  <p>Looking at the example below, one primary difference is the memory-management, which for polymorphism remains on the user-side, and for type-erasure is neatly tucked away requiring some reference-counting (or boost). Having said that, depending on the usage scenarios, the situation might be improved for the polymorphism-example by using smart-pointers with the vector (?), though for arbitrary cases this may very well turn out to be impractical (?). Another aspect, potentially in favor of type-erasure, may be the independence of a common interface, but why exactly would that be an advantage (?).</p>
  
  <p>The code as given below was tested (compiled &amp; run) with MS VisualStudio 2008 by simply putting all of the following code-blocks into a single source-file. It should also compile with gcc on Linux, or so I hope/assume, because I see no reason why not (?) :-) I have split/divided the code here for clarity.</p>
  
  <p>These header-files should be sufficient, right (?).</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;vector&gt;
  #include &lt;string&gt;
  </code></pre>
  
  <p>Simple reference-counting to avoid boost (or other) dependencies. This class is only used in the type-erasure-example below.</p>
  
  <pre><code>class RefCount
  {
    RefCount( const RefCount&amp; );
    RefCount&amp; operator= ( const RefCount&amp; );
    int m_refCount;
  
    public:
      RefCount() : m_refCount(1) {}
      void Increment() { ++m_refCount; }
      int Decrement() { return --m_refCount; }
  };
  </code></pre>
  
  <p>This is the simple type-erasure example/illustration. It was copied and modified in part from the following article. Mainly I have tried to make it as clear and straightforward as possible.
  <a href="http://www.cplusplus.com/articles/oz18T05o/" rel="noreferrer">http://www.cplusplus.com/articles/oz18T05o/</a></p>
  
  <pre><code>class Object {
    struct ObjectInterface {
      virtual ~ObjectInterface() {}
      virtual std::string GetSomeText() const = 0;
    };
  
    template&lt; typename T &gt; struct ObjectModel : ObjectInterface {
      ObjectModel( const T&amp; t ) : m_object( t ) {}
      virtual ~ObjectModel() {}
      virtual std::string GetSomeText() const { return m_object.GetSomeText(); }
      T m_object;
   };
  
    void DecrementRefCount() {
      if( mp_refCount-&gt;Decrement()==0 ) {
        delete mp_refCount; delete mp_objectInterface;
        mp_refCount = NULL; mp_objectInterface = NULL;
      }
    }
  
    Object&amp; operator= ( const Object&amp; );
    ObjectInterface *mp_objectInterface;
    RefCount *mp_refCount;
  
    public:
      template&lt; typename T &gt; Object( const T&amp; obj )
        : mp_objectInterface( new ObjectModel&lt;T&gt;( obj ) ), mp_refCount( new RefCount ) {}
      ~Object() { DecrementRefCount(); }
  
      std::string GetSomeText() const { return mp_objectInterface-&gt;GetSomeText(); }
  
      Object( const Object &amp;obj ) {
        obj.mp_refCount-&gt;Increment(); mp_refCount = obj.mp_refCount;
        mp_objectInterface = obj.mp_objectInterface;
      }
  };
  
  struct MyObject1 { std::string GetSomeText() const { return "MyObject1"; } };
  struct MyObject2 { std::string GetSomeText() const { return "MyObject2"; } };
  
  void UseTypeErasure() {
    typedef std::vector&lt;Object&gt; ObjVect;
    typedef ObjVect::const_iterator ObjVectIter;
  
    ObjVect objVect;
    objVect.push_back( Object( MyObject1() ) );
    objVect.push_back( Object( MyObject2() ) );
  
    for( ObjVectIter iter = objVect.begin(); iter != objVect.end(); ++iter )
      std::cout &lt;&lt; iter-&gt;GetSomeText();
  }
  </code></pre>
  
  <p>As far as I'm concerned, this seems to achieve pretty much the same using polymorphism, or maybe not (?).</p>
  
  <pre><code>struct ObjectInterface {
    virtual ~ObjectInterface() {}
    virtual std::string GetSomeText() const = 0;
  };
  
  struct MyObject3 : public ObjectInterface {
    std::string GetSomeText() const { return "MyObject3"; } };
  
  struct MyObject4 : public ObjectInterface {
    std::string GetSomeText() const { return "MyObject4"; } };
  
  void UsePolymorphism() {
    typedef std::vector&lt;ObjectInterface*&gt; ObjVect;
    typedef ObjVect::const_iterator ObjVectIter;
  
    ObjVect objVect;
    objVect.push_back( new MyObject3 );
    objVect.push_back( new MyObject4 );
  
    for( ObjVectIter iter = objVect.begin(); iter != objVect.end(); ++iter )
      std::cout &lt;&lt; (*iter)-&gt;GetSomeText();
  
    for( ObjVectIter iter = objVect.begin(); iter != objVect.end(); ++iter )
      delete *iter;
  }
  </code></pre>
  
  <p>And finally for testing all of the above together.</p>
  
  <pre><code>int main() {
    UseTypeErasure();
    UsePolymorphism();
    return(0);
  }
  </code></pre>