我想做一个具有两个模板参数的模板类。首先 - 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 语言版本。