title: CRTP and multilevel inheritance
body: <p>A friend of mine asked me <em>"how to use CRTP to replace polymorphism in a multilevel inheritance"</em>. More precisely, in a situation like this:</p>
  
  <pre><code>struct A {
  
    void bar() {
      // do something and then call foo (possibly) in the derived class:
      foo();
    }
  
    // possibly non pure virtual
    virtual void foo() const = 0;
  }
  
  struct B : A {
    void foo() const override { /* do something */ }
  }
  
  struct C : B {
    // possibly absent to not override B::foo().
    void foo() const final { /* do something else */ }
  }
  </code></pre>
  
  <p>My friend and I understand that CRTP is not a drop-in replacement for polymorphism but we are interested in cases where both patterns can be used. (For the sake of this question, we are not interested in pros and cons of each pattern.)</p>
  
  <ol>
  <li><p>This <a href="https://stackoverflow.com/questions/2821012/how-to-write-curiously-recurring-templates-with-more-than-2-layers-of-inheritanc">question</a> has been asked before but it turned out the the author wanted to implement the <em>named parameter idiom</em> and his own <a href="https://stackoverflow.com/a/2821337/1137388">answer</a> focus on this problem more than on the CRTP. On the other hand, the most voted <a href="https://stackoverflow.com/a/2821385/1137388">answer</a> seems to be just about a derived class method calling its homonym in the base class. </p></li>
  <li><p>I came up with an answer (posted below) which has quite a lot of boilerplate code and I wonder if there are simpler alternatives.</p></li>
  </ol>