title: CRTP against Function Overriding
body: <p>CRTP is often suggested to avoid dynamic dispatch in C++, as a replacement for Virtual functions. However, one cannot access the derived class object with a simple base class pointer (without actually knowing the type of the derived object). </p>
  
  <p>In this context, I am wondering, what advantages does CRTP offer against function overriding. To explain my point, I prepared the following example:</p>
  
  <p>CRTP Way of defining a color group base class and a couple of specialization classes:</p>
  
  <pre><code>template &lt;typename D&gt;
  class CG
  {
  public:
      void Green() { GetD()-&gt;GreenColor(); }
      void White() { GetD()-&gt;WhiteColor(); }    
  public:
      void GreenColor() { std::cout &lt;&lt; "This is a simple Green color" &lt;&lt; std::endl; }
      void WhiteColor() { std::cout &lt;&lt; "This is a simple White color" &lt;&lt; std::endl; }
  
  private:
      D* GetD() { return static_cast&lt;D*&gt;(this); }
  };
  
  class ColorGroup1 : public CG&lt;ColorGroup1&gt;
  {
  private:
      friend class CG&lt;ColorGroup1&gt;;
      void GreenColor() { std::cout &lt;&lt; "ColorGroup1: This is Green Shade 1" &lt;&lt; std::endl; }
  };
  
  class ColorGroup20 : public CG
  {
  public:
      void WhiteColor() { std::cout &lt;&lt; "ColorGroup20: This is White Shade 20" &lt;&lt; std::endl; }
  };   
  </code></pre>
  
  <p>The same class as defined above, but using function overriding, instead of CRTP:</p>
  
  <pre><code>class CG
  {
  public:
      void Green()  { std::cout &lt;&lt; "This is a simple Green color"  &lt;&lt; std::endl; }
      void White()  { std::cout &lt;&lt; "This is a simple White color"  &lt;&lt; std::endl; }
  };
  
  class ColorGroup1 : public CG
  {
  public:
      void Green() { std::cout &lt;&lt; "ColorGroup1: This is Green Shade 1" &lt;&lt; std::endl; }
  };
  
  class ColorGroup20 : public CG
  {
  public:
      void White() { std::cout &lt;&lt; "ColorGroup20: This is White Shade 20" &lt;&lt; std::endl; }
  };
  </code></pre>
  
  <p>Now I can use the following code in two different programs, one using the CRTP defined classes and the other with function overriding. </p>
  
  <pre><code>int main()
  {
      ColorGroup1 w;
      w.Green();
      w.White();
  
      std::cout &lt;&lt; std::endl;
  
      ColorGroup20 p;
      p.Green();
      p.White();
  
      return 0;
  }
  </code></pre>
  
  <p>The program result in both the cases:</p>
  
  <pre><code>ColorGroup1: This is Green Shade 1
  This is a simple White color
  
  This is a simple Green color
  ColorGroup20: This is White Shade 20
  </code></pre>
  
  <p>So my question is:</p>
  
  <ol>
  <li>Does CRTP offer any other advantage what function overriding does not offer ?</li>
  <li>Is my assumption correct that both the ways offer equal/similar performance ?</li>
  <li>If I were given a choice I would go for function overriding. What would you choose and why ?</li>
  </ol>
  
  <p>Thanks a lot for your time.</p>