Inherited constructor, compiles in clang++3.9, fails in g++ 7

不羁的心 提交于 2019-12-10 17:17:17

问题


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 cv1 P” (including such a constructor instantiated from a template) is excluded from the set of candidate functions when constructing an object of type cv2 D if the argument list has exactly one argument and C is reference-related to P and P is reference-related to D.

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!