模板递归不会停止

pet*_*ter 2 c++ templates

我想创建模板递归,让我通过使用make_sequence <4>创建序列<0,1,2,3,4>,但似乎递归不会在特化时停止,只是继续运行直到stackoverfolw.

#include <iostream>
#include <cstdio>

template <std::size_t... Indices>
struct sequence {};




template<std:: size_t N, std::size_t ... Indices>
struct make_sequenceAppend{
        using type = typename make_sequenceAppend<N-1, N, Indices...>::type;
};

template<std:: size_t , std::size_t ... Indices>
struct make_sequenceAppend<0ul, Indices...>{
        using type =  typename sequence< Indices...>::type;

};

template <std::size_t N>
struct make_sequence{

        using type = typename make_sequenceAppend<N-1, N>::type;

};


int main()
{
  make_sequence<4>();
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

Sam*_*hik 5

你的专业化声明是错误的.它应该是:

template<std::size_t ... Indices>
struct make_sequenceAppend<0ul, Indices...>{
Run Code Online (Sandbox Code Playgroud)

另外,sequence< Indices...>::type不存在.您的完整专业应该是:

template<std::size_t ... Indices>
struct make_sequenceAppend<0ul, Indices...>{
        using type =  sequence< Indices...>;

};
Run Code Online (Sandbox Code Playgroud)

你的分号中也有一个缺失的分号main(),但这不是重点......