2014-11-21 127 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,所以你必須做一些事情,如:

​​