title: Prevent user from deriving from incorrect CRTP base
body: <p>I cannot think about a proper question title to describe the problem. Hopefully the details below explains my problem clear.</p>
  
  <p>Consider the following code</p>
  
  <pre><code>#include &lt;iostream&gt;
  
  template &lt;typename Derived&gt;
  class Base
  {
      public :
  
      void call ()
      {
          static_cast&lt;Derived *&gt;(this)-&gt;call_impl();
      }
  };
  
  class D1 : public Base&lt;D1&gt;
  {
      public :
  
      void call_impl ()
      {
          data_ = 100;
          std::cout &lt;&lt; data_ &lt;&lt; std::endl;
      }
  
      private :
  
      int data_;
  };
  
  class D2 : public Base&lt;D1&gt; // This is wrong by intension
  {
      public :
  
      void call_impl ()
      {
          std::cout &lt;&lt; data_ &lt;&lt; std::endl;
      }
  
      private :
  
      int data_;
  };
  
  int main ()
  {
      D2 d2;
      d2.call_impl();
      d2.call();
      d2.call_impl();
  }
  </code></pre>
  
  <p>It will compile and run though the definition of <code>D2</code> is intentionally wrong. The first call <code>d2.call_impl()</code> will output some random bits which is expected as <code>D2::data_</code> was not initialized. The second and third calls will all output <code>100</code> for the <code>data_</code>. </p>
  
  <p>I understand why it will compile and run, correct me if I am wrong.</p>
  
  <p>When we make the call <code>d2.call()</code>, the call is resolved to <code>Base&lt;D1&gt;::call</code>, and that will cast <code>this</code> to <code>D1</code> and call <code>D1::call_impl</code>. Because <code>D1</code> is indeed derived form <code>Base&lt;D1&gt;</code>, so the cast is fine at compile time.</p>
  
  <p>At run time, after the cast, <code>this</code>, while it is truly a <code>D2</code> object is treated as if it is <code>D1</code>, and the call to <code>D1::call_impl</code> will modified the memory bits that are supposed to be <code>D1::data_</code>, and output. In this case, these bits happened to be where <code>D2::data_</code> are. I think the second <code>d2.call_impl()</code> shall also be undefined behavior depending on the C++ implementation.</p>
  
  <p>The point is, this code, while intensionally wrong, will give no sign of error to the user. What I am really doing in my project is that I have a CRTP base class which acts like a dispatch engine. Another class in the library access the CRTP base class' interface, say <code>call</code>, and <code>call</code> will dispatch to <code>call_dispatch</code> which can be base class default implementation or derived class implementation. These all will work fine if the user defined derived class, say <code>D</code>, is indeed derived from <code>Base&lt;D&gt;</code>. It will raise compile time error if it is derived from <code>Base&lt;Unrelated&gt;</code> where <code>Unrelated</code> is not derived from <code>Base&lt;Unrelated&gt;</code>. But it will not prevent user write code like above.</p>
  
  <p>The user use the library by deriving from the base CRTP class and providing some implementation details. There are certainly other design alternatives that can avoid the problem of incorrect use as above (for example an abstract base class). But let's put them aside for now and just believe me that I need this design because of some reason.</p>
  
  <p>So my question is that, is there any way that I can prevent the user from writing incorrect derived class as see above. That is, if user write an derived implementation class, say <code>D</code>, but he derived it from <code>Base&lt;OtherD&gt;</code>, then a compile time error shall be raised.</p>
  
  <p>One solution is use <code>dynamic_cast</code>. However, that is expansive and even when it works it is a run-time error.</p>