简体   繁体   English

如何使用完全限定的名称继承基类的构造函数?

[英]How do you inherit constructors of a base class using fully qualified name?

Suppose I have the following class: 假设我有以下课程:

class foo : public std::runtime_error
{};

To bring in the constructors of std::runtime_error into my class, you'd think I could do this: 要将std::runtime_error的构造函数引入我的类,您可能会认为我可以这样做:

using std::runtime_error::std::runtime_error;

However, this doesn't work. 但是,这不起作用。 Normally I'd think to do this: 通常我想这样做:

using super = std::runtime_error;
using super::super;

This does work, but not sure if this is a workaround or the only real solution. 确实可以,但是不确定这是解决方法还是唯一的真正解决方案。 Upon testing with Clang, this works: 在使用Clang进行测试后,此方法有效:

using runtime_error::runtime_error;

However I'm curious why this works since it isn't fully qualified. 但是我很好奇为什么这行得通,因为它不完全合格。 To throw a wrench in the mix, I tried breaking it by introducing multiple inheritance: 为了在混合中使用扳手,我尝试通过引入多重继承来打破它:

namespace n
{
    class runtime_error {};
}

class foo : public std::runtime_error, public n::runtime_error
{
public:
    using runtime_error::runtime_error;
};

Now using runtime_error::runtime_error doesn't work anymore, because it's ambiguous. 现在using runtime_error::runtime_error不再有效,因为它是模棱两可的。 However, the super alias still works (since you explicitly pick the base class you want to inherit from), but could get nasty if you have to define aliases for each inherited type (eg super1 , super2 ). 但是, super别名仍然有效(因为您显式选择了要继承的基类),但是如果必须为每个继承的类型定义别名(例如super1super2 ),则super别名可能会变super1 super2

What is the proper way to inherit constructors from a base class that is in a different namespace, when fully-qualified name syntax (using :: ) is ambiguous with the :: used to specify the member constructor? 当完全限定名称语法(使用:: :)与用于指定成员构造函数的::不明确时,从不同命名空间中的基类继承构造函数的正确方法是什么?

The reason why you have a compilation error when trying to use using std::runtime_exception::std::runtime_exception in the class is because you need to provide name of the constructor. 尝试在类中使用using std::runtime_exception::std::runtime_exception时出现编译错误的原因是,您需要提供构造函数的名称。 And the name of the constructor for std::runtime_exception is not std::runtime_exception , it is runtime_exception - the name of the constructor is the unqualified type. 并且std::runtime_exception的构造函数的名称不是std::runtime_exception ,而是runtime_exception构造函数的名称为非限定类型。 Because of that, correct notation would be 因此,正确的符号将是

using std::runtime_exception::runtime_exception;

Your other solution with type alias work as well, and although it might seem confusing at first - why does it work with type alias, when type alias is indeed std::runtime_exception - it is helpful to remember that type aliasing is not textual substitution, and thus it works as expected. 您使用类型别名的其他解决方案也可以使用,尽管乍看起来似乎很令人困惑-为什么它可以与类型别名一起使用,但当类型别名确实为std::runtime_exception -记住类型别名不是文本替换是有帮助的,因此它可以按预期工作。

Both approaches are valid and which one to choose is totally dependent on you and your code. 两种方法都是有效的,选择哪种方法完全取决于您和您的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM