C++:为什么禁止递归模板化别名?

C++: Why is the recursive templated alias forbidden?

为什么编译失败:

template<typename T, int N>
using vec = vector<vec<T, N - 1>>;
template<typename T>
using vec<0> = T;

虽然只是将它嵌套到一个结构中就可以了:

template<typename T, int N>
struct foo {
    using vec = vector<typename foo<T, N - 1>::vec>;
};
template<typename T>
struct foo<T, 0> {
    using vec = T;
};

如果您可以用更冗长的构造替换它,那么禁止在别名中递归的理由是什么?

参见:https://godbolt.org/g/YtyhvL

What is the rationale for forbidding recursion in aliases if you can just replace it with more verbose construct?

你在那里回答了你自己的问题。你有做你想做的事情的机制。由于定义的别名只是某物的 shorthand,为什么要使语言已经复杂的语法复杂化?

您使用结构来实现机制,并使用别名来提供漂亮的类型名称:

template<typename T, int N>
using vec = typename foo<T,N>::vec;

短小精悍,语言语法更简单。