为什么我们不能重复使用具有不同模板参数的别名模板标识符?

Why can we not reuse an alias template identifier with different template parameters?

本文关键字:参数 标识符 别名 不能 我们 为什么      更新时间:2023-10-16

以以下示例(https://godbolt.org/z/ouX3Vz

(:
template<typename T>
using A = T;
template<typename T, typename V>
using A = V; // Why is this not allowed?
template<typename T>
void B() {}
template<typename T, typename V>
void B() {} // Yet this is allowed?
int main() {
A<int> hello = 10; // Allowed, T=int
A<double> world = 20.0; // Allowed, T=double
// A<int, int> bad = 20; // Not allowed, T=int, V=double?
B<int>();
B<int, int>();
}

由于参数不同,我们允许有两个用于B的函数模板,但是,尽管参数不同,我们不允许有两个用于A的别名模板。

这是标准中的疏忽还是我缺少的理由?是否有任何描述此行为的标准参考?

您可以定义多个具有相同名称的函数模板,因为函数可能会相互重载。如果允许函数重载,但不允许函数模板重载,那么这将是使用模板的严重障碍。

没有必要允许同一范围内的多个类模板具有相同的名称,因为对于这样的功能,单个可变参数模板尚未解决的用例并不多,并且会使语言更加复杂。(例如,考虑以后无法引用集合中的一个特定模板。类似的语句适用于别名模板。