问题
This code snippet
struct Base{};
struct Derived: Base{
using Base::Base;
};
int main()
{
Base b;
Derived d{b};
}
compiles fine on clang++3.9, however it fails on all gcc's (including 7) and clangs with versions smaller than 3.9 with the error msg
error: no matching function for call to 'Derived::Derived() Derived d{b}'.
Is the code above standard compliant or not?
PS: if I comment out the using Base::Base line, the code does not compile anymore on clang-3.9.
回答1:
This is CWG 2356. It appears that both gcc and clang implement this already (clang 4.0+ rejects it), despite it still being "tentatively ready" and not actually adopted into the working draft yet. even thought it was only just adopted into the working draft in Rapperswil last month via P1114.
The rule there is that:
A constructor inherited from class type
C(15.6.3 [class.inhctor.init]) that has a first parameter of type “reference to cv1P” (including such a constructor instantiated from a template) is excluded from the set of candidate functions when constructing an object of type cv2Dif the argument list has exactly one argument andCis reference-related toPandPis reference-related toD.
This excludes the Base copy constructor from consideration (in our case C and P are both B, and B is reference-related to D), which is why both compilers reject your code.
来源:https://stackoverflow.com/questions/51162534/inherited-constructor-compiles-in-clang3-9-fails-in-g-7