继承的构造函数,在clang ++ 3.9中编译,在g ++ 7中失败 [英] Inherited constructor, compiles in clang++3.9, fails in g++ 7

查看:122
本文介绍了继承的构造函数,在clang ++ 3.9中编译,在g ++ 7中失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此代码段

struct Base{};
struct Derived: Base{
    using Base::Base;
};

int main() 
{
    Base b;
    Derived d{b};
}

在clang ++ 3.9上可以正常编译,但是

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

错误:没有匹配的函数可以调用'Derived :: Derived()Derived d {b}'.

error: no matching function for call to 'Derived::Derived() Derived d{b}'.

代码是否符合标准?

PS:如果我注释掉using Base::Base行,则该代码将不再在clang-3.9上编译.

PS: if I comment out the using Base::Base line, the code does not compile anymore on clang-3.9.

推荐答案

这是 CWG 2356 .看来gcc和clang都已经实现了这一点(clang 4.0+拒绝了它),尽管它仍然暂时准备好"并且还没有被实际应用到工作草案中.甚至认为它只是被采纳了.上个月通过 P1114 进入拉珀斯维尔的工作草案.

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.

这里的规则是:

从类类型C(15.6.3 [class.inhctor.init])继承的构造函数,其第一个参数类型为对 cv1 P的引用"(包括此类如果构造参数类型为 cv2 D的对象,并且自变量列表中只有一个自变量且CPD有关.

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.

这将Base复制构造函数排除在考虑范围之外(在我们的情况下,CP都是B,并且BD引用相关),这就是两个编译器都拒绝的原因您的代码.

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.

这篇关于继承的构造函数,在clang ++ 3.9中编译,在g ++ 7中失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆