两次模板<typename t =“”>声明

时间:2018-06-24 14:57:52

标签: c++ templates

我从cppreference

复制了此代码块
template<class T>
struct Alloc { };
template<class T>
using Vec = vector<T, Alloc<T>>; // type-id is vector<T, Alloc<T>>
Vec<int> v; // Vec<int> is the same as vector<int, Alloc<int>>

我不明白为什么template<typename T>宣布两次?

如果template<typename T>小数位数属于以下行,则必须在某种程度上确定范围。

1 个答案:

答案 0 :(得分:1)

模板参数是占位符,它们的范围仅限于一个声明。 因此,{p}中的T

template<class T> struct Alloc { };

未连接到T in

template<class T> using Vec = vector<T, Alloc<T>>;

类似于您可以在不同的函数声明中使用相同的参数名称。