Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 实施C++;用于生成具有给定范围的索引序列的模板_C++_Templates_Metaprogramming_C++14 - Fatal编程技术网

C++ 实施C++;用于生成具有给定范围的索引序列的模板

C++ 实施C++;用于生成具有给定范围的索引序列的模板,c++,templates,metaprogramming,c++14,C++,Templates,Metaprogramming,C++14,因此,C++14提供了结构make_index_sequence,用于生成从0到N-1的索引序列。我想知道如何实现一个在给定范围内生成索引序列的方法。例如: template <size_t Min, size_t Max> struct make_index_range; // make_index_range<5, 9> will give index_sequence<5, 6, 7, 8> 模板 结构生成索引范围; //make_index_r

因此,C++14提供了结构
make_index_sequence
,用于生成从
0
N-1
的索引序列。我想知道如何实现一个在给定范围内生成索引序列的方法。例如:

template <size_t Min, size_t Max>
struct make_index_range;  

// make_index_range<5, 9> will give index_sequence<5, 6, 7, 8>
模板
结构生成索引范围;
//make_index_range将给出索引顺序

您定义
索引范围的方式(在编辑问题之前)答案很简单:

template<std::size_t Min, std::size_t Max>
  using make_index_range = index_range<Min, Max>;
(但使用此选项时,您必须使用
make_index_range::type
,因此别名模板可能更好,更接近
make_index_sequence
的工作方式。)

#include <utility>

template<std::size_t N, std::size_t... Seq>
  constexpr std::index_sequence<N + Seq ...>
  add(std::index_sequence<Seq...>)
  { return {}; }

template<std::size_t Min, std::size_t Max>
  using make_index_range = decltype(add<Min>(make_index_sequence<Max-Min>()));
template<std::size_t Min, std::size_t Max>
  struct make_index_range {
    using type = decltype(add<Min>(make_index_sequence<Max-Min>()));
  };