繁体   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