title: Can I use template member functions as an alternative to the CRTP in order to implement static polymorphism?
body: <p>I want to implement the <a href="https://en.wikipedia.org/wiki/Template_method_pattern" rel="nofollow noreferrer">template method</a> pattern using static polymorphism in C++.</p>
  <p>Right now I have two classes <code>A</code> and <code>B</code> which are very similar. They have an identical public API and the exact same list of private data members. Most of the public member functions are also implemented identically but for some of them the two classes differ slightly in their implementation.</p>
  <p>The situation looks somewhat like this:</p>
  <pre><code>class A {
  public:
    /* Ctor etc... */
    int One() const;
    int Two() const;
    int Three() const; /* Calls PrivateA() internally */
  private:
    int PrivateA() const;
    int a;
    int b;
    int c;
  };
  
  class B {
  public:
    /* Ctor etc... */
    int One() const; /* Identical to A::One() */
    int Two() const; /* Identical to A::Two() */
    int Three() const; /* Same as in A::Three() but calls PrivateB() instead of PrivateA() internally */
  private:
    int PrivateB() const; /* Specific for B */
    /* Identical data members (type and semantics) to A */
    int a;
    int b;
    int c;
  };
  </code></pre>
  <p>I now want to reduce code duplication by moving all the shared code and data members to a base class and delegate to sub-class specific member functions from within the base class'. That is, something like this:</p>
  <pre><code>class Base {
  public:
    int One() const;
    int Two() const;
    int Three() const; /* Should delegate to Private() internally */
  private:
    int a;
    int b;
    int c;
  };
  
  class A : public Base {
  private:
    int Private() const;
  };
  
  class B : public Base {
  private:
    int Private() const;
  };
  </code></pre>
  <p>I know I can solve this using the <a href="https://en.wikipedia.org/wiki/Curiously_recurring_template_pattern" rel="nofollow noreferrer">CRTP</a> in a manner similar to this:</p>
  <pre><code>template&lt;typename T&gt;
  class Base {
  public:
    int One() const;
    int Two() const;
    int Three() const; /* Should delegate to Private() internally */
  private:
    int a;
    int b;
    int c;
  };
  
  class A : public Base&lt;A&gt; {
  private:
    friend class Base&lt;A&gt;;
    int Private() const;
    int p;
  };
  
  class B : public Base&lt;B&gt; {
  private:
    friend class Base&lt;B&gt;
    int Private() const;
    int q;
  };
  
  template&lt;typename T&gt;
  int Base&lt;T&gt;::Three() const {
    return static_cast&lt;const T*&gt;(this)-&gt;Private();
  }
  
  int A::Private() const { return this-&gt;p; }
  int B::Private() const { return this-&gt;q; }
  
  int main() {
    A a{};
    B b{};
  
    /* Uses shared code from base class */
    a.One();
    b.One();
  
    /* Base class code statically dispatches to sub-class code via CRTP */
    a.Three(); /* Returns value of p */
    b.Three(); /* Returns value of q */
  
    return 0;
  }
  </code></pre>
  <p>Now onto my question.</p>
  <p>Is it possible to achieve the same result <strong>without</strong> making <code>Base</code> a template class? Can I accomplish the same thing using e.g. template member functions?</p>
  <pre><code>/* Note: no template */
  class Base {
  public:
    int One() const;
    int Two() const;
  
    template&lt;typename T&gt;
    int Three() const; /* Should delegate to Private() internally */
  private:
    int a;
    int b;
    int c;
  };
  </code></pre>