title: In C++, is it possible to use CRTP with a private base?
body: <p>In C++ I have many classes, unrelated by inheritance, that define a method <code>std::string get_name() const</code>.</p>
  
  <p>There are several utility functions that many classes need that are implemented in terms of get_name().  I would like classes that implement get_name() to also get these utility functions.  To use a silly example let's say that for every class that has get_name, I want to have <code>void print_name() const { std::cout &lt;&lt; get_name() &lt;&lt; std::endl; }</code>.  There are a few such functions, though, and I don't want to repeat them in each class that needs them.</p>
  
  <p>Sounds like a job for CRTP:</p>
  
  <pre><code>template &lt;class T&gt;
  class NameMethods {
  public:
  
    void print_name() const {
       std::cout &lt;&lt; static_cast&lt;const T&amp;&gt;(*this).get_name() &lt;&lt; std::endl;
    }
  
  };
  </code></pre>
  
  <p>Works great.</p>
  
  <p>Here's the wrinkle - what if I want print_name() to be private in the subclass?  If the subclass inherits privately from NameMethods, then the downcast won't compile (inaccessible base).  I could replace it with a reinterpret_cast, a trivial example would work, but a reinterpret_cast won't do the right thing if there's any multiple inheritance, which there might be in the general case.</p>
  
  <p>Is there an easy way to do this sort of thing?</p>
  
  <p>Thanks!</p>
  
  <p>Edit: I should add a little background.  I'm trying to write in a style that uses protocols where possible, rather than abstract superclasses.  If I make an abstract superclass with a public get_name() and a protected print_name(), I could easily get everything to work.  The problem is that I don't want an abstract superclass.  It would have several subclasses that I would like to be unrelated (because I eventually want to compose them using mixins or MI).</p>
  
  <p>My idea is that I will compose a concrete class, and somewhere in that class or in a superclass, something will implement get_name().  That concrete class will want a set of utility functions, but those utility functions shouldn't be part of the interface.</p>