title: C++ Constructor: Perfect forwarding and overload
body: <p>I have two classes, A and B, and B is derived from A.
  A has multiple constructors (2 in the example below).
  B has one additional member to initialize (which has a default initializer).</p>
  
  <p><strong>How can I achieve that B can be construced using one of the constructors of A without having to manually rewrite all the constructor overloads from A in B?</strong></p>
  
  <p>(In the example below, I would otherwise have to provide four constructors for B:<code>B():A(){}</code>, <code>B(string s):A(s){}</code>, <code>B(int b):A(),p(b){}</code>, <code>B(string s, int b):A(s),p(b){}</code>, instead of just two, at least when ignoring the possibility of default arguments).</p>
  
  <p>My approach was perfect forwarding, however, the following scenario leads to an error:</p>
  
  <pre><code>#include &lt;utility&gt;
  #include &lt;string&gt;
  
  struct A {
      A(const std::string&amp; a) : name(a) {}
      A(){}
      virtual ~A(){}
  
      std::string name;
  };
  
  struct B : public A {
      template&lt;typename... Args&gt;
      B(Args&amp;&amp;... args) : A(std::forward&lt;Args&gt;(args)...) {}
  
      B(const std::string&amp; a, int b) : A(a), p(b) {}
  
      int p = 0;
  };
  
  int main()
  {
      B b1("foo");
      B b2("foobar", 1);
  }
  </code></pre>
  
  <p>For b2, GCC complains about <code>no matching function for call to 'A::A(const char [5], int)</code>.
  Apparently it is trying to call the perfect forwarding constructor, which obviously shouldn't work, instead of the second constructor of B.</p>
  
  <p><strong>Why doesn't see the compiler the second constructor and call that instead? Are there technical reasons that the compiler can't find the correct constructor of B?
  How can I fix this behaviour?</strong></p>
  
  <p>The exact error message:</p>
  
  <pre><code>main.cpp: In instantiation of 'B::B(Args&amp;&amp; ...) [with Args = {const char (&amp;)[5], int}]':
  main.cpp:26:19:   required from here
  main.cpp:15:54: error: no matching function for call to 'A::A(const char [5], int)'
       B(Args&amp;&amp;... args) : A(std::forward&lt;Args&gt;(args)...) {}
                                                        ^
  main.cpp:6:5: note: candidate: A::A()
       A(){}
       ^
  main.cpp:6:5: note:   candidate expects 0 arguments, 2 provided
  main.cpp:5:5: note: candidate: A::A(const string&amp;)
       A(const std::string&amp; a) : name(a) {}
       ^
  main.cpp:5:5: note:   candidate expects 1 argument, 2 provided
  main.cpp:4:8: note: candidate: A::A(const A&amp;)
   struct A {
          ^
  main.cpp:4:8: note:   candidate expects 1 argument, 2 provided
  </code></pre>