nya*_*108 6 c++ virtual-inheritance default-constructor template-meta-programming variadic-templates
在以下代码中,编译器请求基类X是默认可构造的.但是,如果我删除虚拟从继承关键字类节点,接入到成员m_x变,当然,暧昧的,但默认构造函数的类X不再需要.
这是什么原因?
#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;
}
Run Code Online (Sandbox Code Playgroud)
类的初始化顺序如下[class.base.init]:
在非委托构造函数中,初始化按以下顺序进行:
- 首先,仅对于派生程度最高的类(1.8)的构造函数,虚拟基类按它们在深度优先左侧出现的顺序进行初始化.正确遍历基类的有向非循环图,其中"从左到右"是派生类base-specifier-list中基类出现的顺序.
你的层次结构是A --> Node<X> --> X,所以初始化的第一件事是X,因为它是一个虚拟的基类.它没有在mem-initializer中指定,因此插入了隐式默认构造:
A( int i )
: X() // <== implicit
, Node< X >( i )
, m_a( i )
{
}
Run Code Online (Sandbox Code Playgroud)
由于X不是默认构造,因此您会收到该错误.你可以通过明确提供正确的东西来解决这个问题:
A( int i )
: X(i)
, Node< X >( i )
, m_a( i )
{
Run Code Online (Sandbox Code Playgroud)
您不必担心X被构造两次,因为虚拟基类只是为最派生的类构建的......这将是A和不是Node<X>.
从@Berry的回答开始,修复代码的唯一方法是编写对虚拟继承的X构造函数的显式调用。
然而,在类A、B或C中显式调用 X 的构造是不够的:基本上必须在涉及任何级别继承的每个类中调用它!
棘手的是Inheritance<>可变参数模板类:可变参数扩展的每一步都必须提供对 X 构造函数的显式调用。
以下是在启用了 C++11 标志的MinGW 4.9.2上运行的代码:
#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;
}
Run Code Online (Sandbox Code Playgroud)