【发布时间】:2013-01-21 11:32:48
【问题描述】:
我想创建在内部使用特定容器来确定不同类型的通用类模板。像这样的:
#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;
最自然的方法是什么?比如说,你是否想以任何形式提及容器分配器的存在?
【问题讨论】:
-
我建议你最好不要这样做。虽然 STL 容器在接口上相似,但它们必然相同。此外,使用一个容器的最有效方式不一定是使用另一个容器的最有效方式。我认为您不太可能成功地模板化您的存储而不会遇到困难。你为什么要这样做?你想达到什么目标?
-
我想在访问元素的速度(允许向量)和插入速度(提供列表)之间做出选择。