2014-11-21 137 views
1

我想要一个node类来构建树。 node是一个模板类,模板论证_Data_Container。对于模板论证中没有递归,我让_Container成为一个类的模板类实例。我在node中声明typedef _Data data_type,并且想要使用node::container_type,如node::data_type。我能怎么做?C++:将模板声明为类成员

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; 
}; 

回答

2

在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,所以你必须做一些事情,如:

​​