簡體   English   中英

C ++:將模板聲明為類成員

[英]c++: declare template as a class member

我想要一個node類來構建一棵樹。 node是具有模板爭論_Data_Container的模板類。 為了避免模板爭執的遞歸,我讓_Container作為代表類的模板類。 我在node聲明typedef _Data data_type ,並想使用node::container_typenode::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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM