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

Kub*_*uba 2 c++ templates stl

我想创建一个具有两个模板参数的模板类.首先 - 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;
}
Run Code Online (Sandbox Code Playgroud)

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

In function 'int main()':
15:9: error: missing template arguments before 'f'
Run Code Online (Sandbox Code Playgroud)

我想foo等同于foo<int, std::vector>宣言.

我的班级定义在哪里?

JVA*_*pen 5

使用C++ 14或更早版本,您需要编写foo<>以实例化模板.

从C++ 17开始,由于Class Template Argument Deduction,它实际上就像你编写它一样工作.-std=c++17如果您的编译器支持,您可以考虑更新C++语言版本.