虚拟继承是否强制基类默认可构造? [英] Does virtual inheritance force a base class to be default constructible?

本文介绍了虚拟继承是否强制基类默认可构造?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在以下代码中,编译器要求基础类 X默认可构造.但是,如果我从 类 Node 的继承中删除 virtual 关键字,对成员 m_x 的访问当然变得不明确,但不再需要类 X默认构造函数.

In the following code, the compiler is requesting the base class X to be default constructible. However, if I remove the virtual keyword from the inheritance of the class Node, the access to the member m_x becomes, of course, ambiguous, but the default constructor for class X is no longer required.

这是什么原因?

#include <iostream>

struct Apply
{
    template< typename T >
    struct Node : virtual T    // this line contains the virtual inheritance
    {
        template< typename ...Args>
        Node( Args... args )
            : T( args... )
        {}
    };

    template < typename ...BaseClasses>
    struct Inheritance;

    template < typename FirstBaseClass, typename ...OtherBaseClasses>
    struct Inheritance< FirstBaseClass, OtherBaseClasses... >   : FirstBaseClass
            , Inheritance< OtherBaseClasses... >
    {
        template< typename ...Args>
        Inheritance( Args... args )
            : FirstBaseClass( args... )
            , Inheritance< OtherBaseClasses... >( args... )
        {

        }
    };
};

template < >
struct Apply::Inheritance< >
{
    template< typename ...Args>
    Inheritance( Args... args ){}
};

struct X
{
    X(int i){}

    int m_x;
};

struct A : Apply::Node< X >
{
    A( int i )
        : Apply::Node< X >( i )
        , m_a( i )
    {

    }
    int m_a;
};


struct B : Apply::Node< X >
{
    B( int i )
        : Apply::Node< X >( i )
        , m_b( i )
    { }

    int m_b;
};

struct C : Apply::Node< X >
{
    C( int i )
        : Apply::Node< X >( i )
        , m_c( i )
    { }

    int m_c;
};

struct Example : Apply::Inheritance< A, B, C >
{
    Example( int i )
        : Apply::Inheritance< A, B, C >( i )
    { }

    void print( ) const
    {
        // this line needs the virtual inheritance
        std::cout << m_x << std::endl;

        std::cout << m_a << std::endl;
        std::cout << m_b << std::endl;
        std::cout << m_c << std::endl;
    }
};

int main()
{
    Example ex( 10 );

    ex.print( );

    return 0;
}

推荐答案

从@Berry 的回答开始,修复代码的唯一方法是编写对虚拟继承的 X 构造函数的显式调用.

Starting from @Berry answer, the only way to fix the code was to code an explicit call to the virtual inherited X constructor.

然而,在ABC类中显式调用X的构造是不够的:它必须被调用基本上每个类都涉及到任何级别的继承!

However, it is not enough to explicit call the construction of X in classes A, B, or C: it must be called basically in every class involved in the inheritance at any level!

棘手的是继承<>可变参数模板类:可变参数扩展的每一步都必须提供对X构造函数的显式调用.

The tricky one was the Inheritance<> variadic template class: every step of the variadic expansion must provide the explicit call to the X constructor.

这是在启用 C++11 标志的 MinGW 4.9.2 上运行的代码:

Here is the code that works on MinGW 4.9.2 with enabled C++11 flag:

#include <iostream>

template< typename T, typename V >
struct Node : virtual V
{
    using Virtual = V;    // Added this line

    template< typename ...Args >
    Node( Args... args )
        : V( args... )
    { }
};

template < typename ...BaseClasses>
struct Inheritance;

template < typename FirstBaseClass, typename ...OtherBaseClasses>
struct Inheritance< FirstBaseClass, OtherBaseClasses... >
        : FirstBaseClass
        , Inheritance< OtherBaseClasses... >
{
    template< typename ...Args>
    Inheritance( Args... args )
        : FirstBaseClass::Virtual( args... )    // added this line
        , FirstBaseClass( args... )
        , Inheritance< OtherBaseClasses... >( args... )
    { }
};

template < >
struct Inheritance< >
{
    template< typename ...Args >
    Inheritance( Args... args )
    { }
};

struct X
{
    X(int i)
        : m_x( i )
    { }

    int m_x;
};

struct A : Node< A, X >
{
    A( int i )
        : X( i )    // added this line
        , Node< A, X >( i )
        , m_a( i )
    { }

    int m_a;
};


struct B : Node< B, X >
{
    B( int i )
        : X ( i )    // added this line
        , Node< B, X >( i )
        , m_b( i )
    { }

    int m_b;
};

struct C : Node< C, X >
{
    C( int i )
        : X ( i )    // added this line
        , Node< C, X >( i )
        , m_c( i )
    { }

    int m_c;
};

struct Example : Inheritance< A, B, C >
{
    Example( int i )
        : X ( i )    // added this line
        , Inheritance< A, B, C >( i )
    { }

    void print( ) const
    {
        // this line needs the virtual inheritance
        std::cout << m_x << std::endl;

        std::cout << m_a << std::endl;
        std::cout << m_b << std::endl;
        std::cout << m_c << std::endl;
    }
};

int main()
{
    Example ex( 10 );

    ex.print( );

    return 0;
}

这篇关于虚拟继承是否强制基类默认可构造?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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