您现在的位置是:首页 > 编程语言学习 > 其他编程语言 > 文章正文 其他编程语言

C++模板index_sequence使用示例详解

2022-12-08 10:02:21 其他编程语言

简介integer_sequence 是 c++ 14中新增加的一个元编程工具,其衍生出来的还有如 index_sequence、make_index_sequence、index_sequence_for...

integer_sequence 是 c++ 14中新增加的一个元编程工具,其衍生出来的还有如 index_sequencemake_index_sequenceindex_sequence_for等辅助工具,现在,让我们来浅尝一下这些东西吧!

integer_sequence

integer_sequence 其实没有什么特殊的,就是一个类

只不过他是index_sequence的基础

  1. template<typename _Tp, _Tp... _Idx> 
  2.   struct integer_sequence 
  3.   { 
  4. typedef _Tp value_type; 
  5. static constexpr size_t size() noexcept { return sizeof...(_Idx); } 
  6.   }; 

index_sequence

index_sequence 会在编译期生成一个从0开始的序列

然后你就可以对其进行一些奇奇怪怪的操作

  1. template <size_t... N> void print(std::index_sequence<N...>) { 
  2.   std::vector<int> res; 
  3.   (void)std::initializer_list<int>{ 
  4.   ((res.push_back(N), std::cout << N << " "), 0)...}; 
  5.   std::for_each(res.begin(), res.end(), [](int x) {std::cout << x << " ";}); 
  6. int main() { 
  7.   auto t = std::make_index_sequence<10>(); 
  8.   print(t); 
  9.   return 0; 

比如 ((res.push_back(N), std::cout << N << " "), 0)...这句话,就会在编译期被展开

这里展开在了一个初始化列表中,小技巧

make_index_sequence

那么,index_sequence 是如何生成的呢?

有两种形式

  • 编译器内建
  • 递归式的生成

第二种方式嘛还是用到了元编程的惯用伎俩,特化,递归式的编程

  1. template <int... N> struct index_seq {}; 
  2. template <int N, int... M> 
  3. struct make_index_seq : public make_index_seq<N - 1, N - 1, M...> {}; 
  4. template <int... M> struct make_index_seq<0, M...> : public index_seq<M...> {}; 

对齐使用也是一样的形式

  1. template <int... N> void print(index_seq<N...>) { 
  2.   (void)std::initializer_list<int>{((std::cout << N << " "), 0)...}; 
  3. int main() { 
  4.   auto r = make_index_seq<100>(); 
  5.   print(r); 
  6.   return 0; 

使用场景

刚才,看见了print去打印的时候打印了 0-(N-1)的元素

那么,这个行为是在编译期展开的,我们就可以用到其他需要常量的地方

比如一个简单的需求:

打印tuple

  1. template <typename T, typename F, int...N> 
  2. void exec_for_tuple(const T& tup, F&& func, index_seq<N...>) { 
  3.   (void)std::initializer_list<int> { 
  4.   (func(std::get<N>(tup)), 0)... 
  5.   }; 
  6. template <typename Func, typename ...Arg> 
  7. void for_tuple(Func&& func, std::tuple<Arg...> tuple) { 
  8.   exec_for_tuple(tuple, std::forward<Func>(func), make_index_seq<sizeof...(Arg)>()); 

exec_for_tuple部分应该非常好懂,但是为什么中间还要再转发一层呢?

因为tuple元素的个数我们不能直接获取到,我们写的又要是一个通用的函数

所以要通过 sizeof...(arg) 这种伎俩来将其元素个数计算出来

如何调用呢?

如下所示:

  1. std::tuple<intintdouble> tuple{1, 2, 3.0}; 
  2. for_tuple([](auto t) { 
  3.   std::cout << t << " "
  4. }, tuple); 

index_sequence_for

那么,看到现在,你知道 index_sequence_for又是何物吗?

其实吧,刚才就已经见过 index_sequence_for这个东西了

其实就是计算可变长模板参数的个数,然后将其长度做成一个sequence出来

  1. template<typename... _Types> 
  2. using index_sequence_for = make_index_seq<sizeof...(_Types)>; 

结语

index_sequence 是一个不起眼的家伙,可能你平时都不会去了解它,但是这个东西的用途还是有很多的,你想想,编译器制造一串序列的能力,在tuple这种模板中,使用其是不是更加方便了,在bind这种模板中的参数上,若是使用它,是不是更加灵活好些了。

其实和tuple一个道理,在平常编程中,你也许不会使用它,但是在模板编程中,这是比较好的一个工具。

相关文章

站点信息