具有构造函数继承的虚拟继承

Virtual Inheritance with Constructor Inheritance

我有一个 class 层次结构,归结为

class Module { };

struct Port {
    Module& owner;
    Port(Module& owner) : owner(owner) {}
};

struct InPort    : virtual Port    { using Port::Port; };
struct OutPort   : virtual Port    { using Port::Port; };
struct InOutPort : InPort, OutPort { using Port::Port; };

如您所见,我更愿意创建一些基本功能,并以 classic 菱形模式继承它。我还想使用构造函数继承使其尽可能地成为未来的证明...

然而,this does not work as written down above

prog.cpp: In function 'int main()':
prog.cpp:14:15: error: use of deleted function 'InOutPort::InOutPort(Module&)'
  InOutPort p(m);

甚至将 InOutPort 的定义替换为更明确的版本 is not enough:

struct InOutPort : InPort, OutPort { InOutPort(Module& m) : Port(m), InPort(m), OutPort(m) { } };

改为I seem to have to write down everything explicitly for it to work::

struct InPort    : virtual Port    { InPort(Module& m) : Port(m) { } };
struct OutPort   : virtual Port    { OutPort(Module& m) : Port(m) { } };
struct InOutPort : InPort, OutPort { InOutPort(Module& m) : Port(m), InPort(m), OutPort(m) { } };

有没有一种方法可以将构造函数继承与我忽略的虚拟继承结合起来?
如果不是,您会使用什么替代方案?
也许可变参数模板构造函数可以完美地将其参数转发给所有基数?

似乎没有办法做这样的事情。在 12.9/8:

...An implicitly-defined inheriting constructor performs the set of initializations of the class that would be performed by a user-written inline constructor for that class with a mem-initializer-list whose only mem-initializer has a mem-initializer-id that names the base class denoted in the nested-name-specifier of the using-declaration and an expression-list as specified below...

换句话说,您继承其构造函数的 class 是 只有 基 class 将参数转发给它。所有其他基础 classes 都需要有默认构造函数。由于您通过继承父构造函数将这些默认构造函数隐藏在中间 classes 中,因此一旦显式继承父构造函数就无法调用它们。

我认为你应该能够为两个中间 classes 使用继承的构造函数,并且只为最派生的 class 编写显式版本 [我没有看到你已经尝试过这个- 从我对标准的理解来看,这确实像是一个编译器错误]。如果我想到另一种更好的方法,我会更新。