[英]How to forward declare a template class and use it as a member data in C++?
[英]c++: declare template as a class member
我想要一个node
类来构建一棵树。 node
是具有模板争论_Data
和_Container
的模板类。 为了避免模板争执的递归,我让_Container
作为代表类的模板类。 我在node
声明typedef _Data data_type
,并想使用node::container_type
如node::data_type
。 我能怎么做?
template<
typename
_Data,
template<typename T> class
_Container = std::vector>
struct node
{
typedef _Data data_type;
//TODO container_type
typedef node<_Data, _Container> type;
typedef std::shared_ptr<type> ptr;
typedef _Container<ptr> ptrs;
Data data;
ptrs children;
};
在C ++ 11中,您可以使用类似以下的内容:
template<typename T, template<typename> class Container>
struct node
{
using data_type = T;
template <typename U>
using container_templ = Container<U>;
};
请注意, container_templ
不是类型(而container_templ<T>
是)。
还要注意, std::vector
与模板Container
不匹配,因为std::vector
需要一个额外的(默认)参数Allocator
,因此您必须执行以下操作:
template <typename T>
using My_Vector = std::vector<T>;
template<typename T, template<typename> class Container = My_Vector>
struct node;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.