c++ - typedef 的 STL 容器类型作为模板参数

标签 c++ templates stl

我想知道是否可以使用 typedef 容器作为模板参数。我正在尝试以下操作:

template<typename T>
using containerT = std::vector<T>;

template <template<class T, class = std::allocator<T> > class container_type = containerT >
struct nodeData {
    container_type<int> param;
};

int main()
{    
    nodeData<> nd;
}

这会导致 GCC 4.8 出现编译错误:

expected a template of type 'template class container_type', got 'template using containerT = std::vector< T >'

有人知道这样做的方法吗?

谢谢

最佳答案

containerT只有一个模板参数,所以template模板参数必须匹配:

template <template<class> class container_type = containerT >

但是,我怀疑您希望能够将标准容器作为模板参数,所以您希望这样:

template<class T, class Allocator = std::allocator<T>>
using containerT = std::vector<T, Allocator>;

现在您可以使用 containerT 作为您在问题中提供的函数模板的模板参数。

关于c++ - typedef 的 STL 容器类型作为模板参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24274056/

相关文章:

c++ - 要学习STL,关于c++应该了解什么?

c++ - 为什么 std::erase(std::erase_if) 不是 <algorithm> 中适用于任何容器的模板?

c++ - C语言中宏的使用

python - Flask 模板在本地环境中工作,但在 nginx 下返回错误 404

templates - Eigen 中 PlainObjectBase 和 DenseBase 的区别

C++ 使用模板来避免编译器检查 boolean 值

c++ - OpenCL - OpenGL - Interop:如何填充 cl::ImageGL

c++ - unsigned int 值没有给出正确的结果

c++ - VS 2012/VC11 中的对齐和 STL

c++ - 当 std::vector 重新分配其内存数组时,使用的是复制构造函数还是移动构造函数?