如何将std :: vector设置为模板的默认参数

How to set std::vector as default parameter of template?

本文关键字:参数 默认 设置 std vector      更新时间:2023-10-16

我想做一个具有两个模板参数的模板类。首先 - n 是一类变量默认设置为int和第二容器stl的容器,默认值设置为std::vector

#include <iostream>
#include <vector>
template <class N=int, 
      template <class T=N, class Allocator=std::allocator<N>> 
      class container=std::vector>
class foo{
    container<N> cont;
};
int main() 
{
    foo f;
}

当我创建上面类的对象f没有模板参数时,编译器写了一个foring错误:

In function 'int main()':
15:9: error: missing template arguments before 'f'

我希望foo等于foo<int, std::vector>声明。

我的班级定义在哪里?

使用C 14或之前,您需要编写foo<>才能实例化模板。

从c 17上开始,它实际上只是因为class模板参数扣除而撰写时,它的作用。如果您的编译器支持它,则可以考虑使用-std=c++17更新C 语言版本。