title: CRTP Base to Derived Conversion
body: <p>Assume I have the following simple class hierarchy using classic inheritance:</p>
  
  <pre><code>struct A_classic {};
  struct B_classic : A_classic {};
  </code></pre>
  
  <p>I want to implement a conversion operator from <code>A_classic</code> to <code>B_classic</code>. To reuse as much code as possible, I do</p>
  
  <pre><code>A_classic a; // Given as input argument
  B_classic b;
  static_cast&lt;A_classic&amp;&gt;(b) = a; // Copy A_classic's members
  // Now set up B_classic's members
  </code></pre>
  
  <p>The problem is that actually I'm using CRTP for inheritance:</p>
  
  <pre><code>template&lt;class Derived&gt; struct A_crtp_base {};
  struct A_crtp : A_crtp_base&lt;A_crtp&gt; {};
  template&lt;class Derived&gt; struct B_crtp_base : A_crtp_base&lt;B_crtp_base&lt;Derived&gt;&gt; {};
  struct B_crtp : B_crtp_base&lt;B_crtp&gt; {};
  </code></pre>
  
  <p>The above trick no longer works, because the "common" base class of <code>A_crtp</code>, <code>B_crtp</code> is <code>A_crtp_base&lt;A_crtp&gt;</code> and <code>A_crtp_base&lt;B_crtp&gt;</code> respectively. </p>
  
  <pre><code>A_crtp a;
  B_crtp b;
  static_cast&lt;A_crtp_base&lt;???&gt;&amp;&gt;(b) = a; 
  // No matter what I put here, either the cast or the assignment will fail
  </code></pre>
  
  <p>An obvious solution is to template the copy constructor of <code>A_crtp_base</code>:</p>
  
  <pre><code>template&lt;class Derived&gt;
  struct A_crt_base {
     template&lt;class OtherDerived&gt;
     A_crtp_base(const A_crtp_base&lt;OtherDerived&gt;&amp; other);
  }
  </code></pre>
  
  <p>But then I have to write my own copy constructor, which I would like to avoid. </p>
  
  <p>Any suggestions how to minimize the amount of coding here?</p>