C ++: Why is recursive template alias not allowed?

Why this fails to compile:

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

while just embedding it in a structure works fine:

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;
};

What is the reason for prohibiting recursion in aliases if you can simply replace it with a more detailed construction?

see: https://godbolt.org/g/YtyhvL

+4
source share
1 answer

What is the reason for prohibiting recursion in aliases if you can simply replace it with a more detailed construction?

You seem to have answered your question. You have a mechanism to do what you want. And since a pseudonym by definition is intended only for shorthand, why complicate languages ​​that were already complex grammar?

You use a structure to implement the machine and an alias to give a nice type name:

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

, .

+2

Source: https://habr.com/ru/post/1686988/


All Articles