繁体   English   中英

STL容器类型作为模板参数

[英]STL container type as template parameter

我想创建在内部使用特定容器确定不同类型的通用类模板。 像这样:

#include <vector>
#include <list>
template< typename F, template< class ... > class container_type = std::vector >
struct C
{
    C();
    template< typename U >
    C(container_type< U >);
    C(container_type< F >);
    C(container_type< int >);
    container_type< double > param;
};
C< unsigned, std::list > c;

最自然的方法是什么? 说,您是否要提及以任何形式出现的容器分配器?

这样的东西?

template< typename F, template<class T, class = std::allocator<T> > class container_type = std::vector >
struct C
{
    C() {}
    template< typename U >
    C(container_type< U >) {}
    C(container_type< F >) {}
    C(container_type< int >) {}

    container_type< double > param;
};

C< unsigned, std::list > c;

编辑:

std::queue使用类似但稍微简单的方法,该方法由将在内部使用的容器类型参数化。 希望这证明这种方法是很自然的。 上面的示例在VC ++ 10中进行了测试,并显示了如何处理分配器。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM