title: CRTP & copy/move assignment/constructor inheritance
body: <p>I am trying to implement a move/copy assignment operators and constructors in a base class for the derived classes using CRTP.</p>
  
  <pre><code>template &lt;typename Derived&gt;
  class base {
  public:
      Derived&amp; operator= (const Derived&amp; other) {
          // Copy the base properties:
          this-&gt;foo_ = other.foo_;
          // ...
          // Continue as the derived class demands it:
          this-&gt;derived().copy(other);
          return this-&gt;derived();
      }
      // etc. for copy/move assignment/construction...
  
  private:
      // To act as the derived class:
      Derived&amp; derived () { return *static_cast&lt;Derived*&gt;(this); }
      const Derived&amp; derived () const { return *static_cast&lt;const Derived*&gt;(this); }
  
  protected:
      // Some base properties:
      int foo_;
      // ...
  };
  
  class derived: public base&lt;derived&gt; {
      friend base&lt;derived&gt;;
  
  public:
      // Inheriting the constructors and assignment operators:
      using base&lt;derived&gt;::base;
      using base&lt;derived&gt;::operator=;
  
  private:
      void copy (const derived&amp; other) {
          // Copy all the needed derived properties:
          this-&gt;bar_ = other.bar_;
          // ...
      }
  
      // Some derived properties:
      int bar_;
      // ...
  };
  
  // To test it:
  int main () {
      derived d, t;
      d = t;
  }
  </code></pre>
  
  <p>Compiler gives me an error, saying that <code>derived&amp; derived::operator=(const derived&amp;)</code> cannot be overwritten with <code>derived&amp; base&lt;derived&gt;::operator=(const derived&amp;)</code>. My theory is, that somehow <code>derived::operator=</code> gets defined implicitly and then by introducing the <code>base&lt;derived&gt;::operator=</code> by the <code>using</code> declaration I'm trying to redefine it again maybe? This looks suspiciously similar to errors that come up when accidentally defining a method twice.</p>
  
  <p>I compiled this with GCC and the full log is:</p>
  
  <pre><code>test.cpp: In function 'int main()':
  test.cpp:25:7: error: 'constexpr derived&amp; derived::operator=(const derived&amp;)' cannot be overloaded
   class derived: public base&lt;derived&gt; {
         ^~~~~~~
  test.cpp:4:14: error: with 'Derived&amp; base&lt;Derived&gt;::operator=(const Derived&amp;) [with Derived = derived]'
       Derived&amp; operator= (const Derived&amp; other) {
                ^~~~~~~~
  </code></pre>
  
  <p>Is this even possible to accomplish, or do I have to define the operators/constructors in the <code>derived</code> class and then delegate their functionality to the <code>base</code> class inside the definition?</p>
  
  <h2>Update</h2>
  
  <p>OK, maybe after looking at this with a clearer mind, it seems overly complicated. I could just do the following:</p>
  
  <pre><code>Derived&amp; base&lt;Derived&gt;::operator= (const base&amp; other) {
      this-&gt;foo_ = other.foo_;
      return this-&gt;self();
  }
  </code></pre>
  
  <p>So the returned type is correct for every derived class and the copy is performed from the base class - only the base properties are copied, which is all I need by default. If I need more, then it's specific to each derived class:</p>
  
  <pre><code>// Adding this to the base class - for any derived class to act as the base one:
  template &lt;Derived&gt;
  base&lt;Derived&gt;&amp; base&lt;Derived&gt;::base () { *return static_cast&lt;base&lt;Derived&gt;*&gt;(this); }
  
  derived&amp; derived::operator= (const derived&amp; other) {
      this-&gt;base() = other.base();
      this-&gt;bar_ = other.bar_;
  }
  </code></pre>
  
  <p>But still, it's an interesting excercise and the question regarding the compiler error remains unanswered.</p>