我想创建泛型类模板,在内部使用特定容器来确定不同的类型.像这样的东西:
#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;
Run Code Online (Sandbox Code Playgroud)
这种方式最自然的方法是什么?比如说,您是否想以任何形式提及容器的分配器的存在?
这样的事情?
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;
Run Code Online (Sandbox Code Playgroud)
编辑:
使用类似但更简单的方法,通过std::queue内部使用的容器类型对其进行参数化.希望这证明这种方法很自然.上面的示例在VC++ 10中进行了测试,并展示了如何处理分配器.