title: C++ Copy/Move constructor in policy based design
body: <p>While exploring Policy Based Design pattern in C++, I stumbled upon one problem which I could not find a solution to: How do you write copy and move constructors for policy based classes in a generic way without referring to member variables inside the policy class?</p>
  
  <p>Here's an example:</p>
  
  <pre><code>class Foobase {
  public:
    Foobase(int v) :val(v) { }
  protected:
    int val;
  };
  
  template &lt;typename Base&gt;
  class Foo : public Base {
  public:
    Foo(int v):Base(v) { }
    // how can I initialize Base() class without referring to it's protected members?
    Foo(const Foo&lt;Base&gt;&amp; other) :Base(other.val) { }
  };
  
  int main() {
    Foo&lt;Foobase&gt; foo(5);
    auto foo2 = foo;
  }
  </code></pre>
  
  <p>In the above code, copy constructor of <code>class Foo</code> uses protected member variable to initialize <code>Base</code> class. Is there any other way to initialize <code>Base</code> except above? What are the best practices in such cases?</p>
  
  <p>Updated question:</p>
  
  <p>Answer by @LogicStuff clarifies copy constructor part of the question, but doesn't answer the move constructor question. See the updated example code in which <code>class Foo</code> can also have member variables.</p>
  
  <pre><code>class Foobase {
  public:
    Foobase(int v) :val(v) { }
    Foobase(const Foobase&amp;) = default;
    Foobase(Foobase&amp;&amp;) noexcept = default;
    Foobase&amp; operator= (const Foobase&amp;) = default;
    Foobase&amp; operator= (Foobase&amp;&amp;) noexcept = default;
    void Print() const {
      std::cout &lt;&lt; val &lt;&lt; std::endl;
    }
  protected:
    int val;
  };
  
  template &lt;typename Base&gt;
  class Foo : public Base {
  public:
    // works fine
    Foo(std::string str, int v):Base(v), name(str) { }
  
    // works fine
    Foo(const Foo&lt;Base&gt;&amp; other) :Base(other), name(other.name) { }
  
    // I'm doubtful about this c'tor, if I move `other` for initializing Base,
    // how can I initialize `name` variable?
    Foo(Foo&lt;Base&gt;&amp;&amp; other)
      :Base(std::move(other)), name(std::move(other.name) /* will this be valid?? */) { }
  
    // can we have copy and assignment operator for this class?
  
    void Print() {
      std::cout &lt;&lt; "name = " &lt;&lt; name &lt;&lt; std::endl;
      Base::Print();
    }
  
  private:
    std::string name;
  };
  
  int main() {
    Foo&lt;Foobase&gt; foo("foo", 5);
    auto foo2 = std::move(foo);
    foo2.Print();
  }
  </code></pre>