1

The following code compiles:

struct A {
    A(int xx) : x(xx) {}
    int x;
};

struct B : A {
    using A::A;
};

int main() {
    B b(5); // OK. The constructor taking an int is available
    return 0;
}

The following code does not:

struct A {
    A() {}
    A(const A &) {}
};

struct B : A {
    using A::A;
};

int main() {
    A a;
    B b; // No complaint about this line
    B c(a); // No constructor taking const A & is available.
    return 0;
}

Why is it that the constructor of A is made available in the first case, but not in the second?

EDIT (following being marked as duplicate):

The answer to the question to which this one is suspected to be a duplicate states (bold is suggested by the comment):

For each non-template constructor in the candidate set of inherited constructors [..], a constructor is implicitly declared with the same constructor characteristics unless there is a user-declared constructor with the same signature in the complete class where the using-declaration appears or the constructor would be a default, copy, or move constructor for that class.

This does not seem to answer my question, since the constructor taking a const A & argument would not be a copy constructor for B.

AlwaysLearning
  • 7,257
  • 4
  • 33
  • 68
  • @LogicStuff The question is not about the default constructor, but about the other one. – AlwaysLearning Jul 31 '16 at 15:01
  • 1
    Yeah, but you'll find your answer written in bold. – LogicStuff Jul 31 '16 at 15:02
  • @LogicStuff It's very difficult for me to parse the text of the standard, so I might be saying nonsense, but I think it does not apply in this situation, since `B` does not have any user-defined constructors of its own. – AlwaysLearning Jul 31 '16 at 15:09
  • **unless** *"there is a user-declared constructor with the same signature in the complete class where the using-declaration appears" **or** *"the constructor would be a default, copy, or move constructor for that class"*. You can ignore the first part, if the second part is true, the statement is true. – LogicStuff Jul 31 '16 at 15:13
  • @LogicStuff But the constructor taking `const A &` would not be a copy constructor for `B`! – AlwaysLearning Jul 31 '16 at 15:19
  • Probably the signature will be transformed into the signature for `B`'s copy constructor. You can ask @0x499602D2. I'm interested too. – LogicStuff Jul 31 '16 at 15:25
  • Yeah, this is badly closed. The answer can be found [class.inhctor]/p3, but it's the part that's omitted in that other answer (the part replaced by just "[..]") that's relevant here. Would [this question and answer](http://stackoverflow.com/questions/22534997/constructor-inheritance-and-custom-constructors) function better as a duplicate, or does it still not completely answer your question? –  Jul 31 '16 at 15:25
  • @hvd So, the answer is amazingly simple: copy constructors are not inherited by the `using` declaration. :) – AlwaysLearning Jul 31 '16 at 15:29

0 Answers0