Yorek B
Yorek B

Reputation: 173

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

Take the following example (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>();
}

We are allowed to have two function templates for B as the parameters differ, however, we are not allowed to have two alias templates for A despite differing parameters.

Is this an oversight in the standard or is there a rationale that I am missing? Are there any references to the standard describing this behavior?

Upvotes: 1

Views: 167

Answers (1)

Brian Bi
Brian Bi

Reputation: 119307

You're allowed to define multiple function templates with the same name because functions can be overloaded with each other. If functions were allowed to overload but function templates were not, then it would be a serious obstacle in the way of using templates.

There was no need to allow multiple class templates in the same scope to have the same name, as there are not many use cases for such a feature that are not already solved by a single variadic template, and it would make the language more complicated. (Consider for example the inability to later refer to one particular template from the set.) A similar statement applies to alias templates.

Upvotes: 3

Related Questions