日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

C++模板?index_sequence使用示例詳解_C 語言

作者:amjieker ? 更新時間: 2023-01-05 編程語言

引言

integer_sequence 是 c++ 14中新增加的一個元編程工具

其衍生出來的還有如 index_sequencemake_index_sequenceindex_sequence_for等輔助工具

現在,讓我們來淺嘗一下這些東西吧!

integer_sequence

integer_sequence 其實沒有什么特殊的,就是一個類

只不過他是index_sequence的基礎

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

index_sequence

index_sequence 會在編譯期生成一個從0開始的序列

然后你就可以對其進行一些奇奇怪怪的操作

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

比如 ((res.push_back(N), std::cout << N << " "), 0)...這句話,就會在編譯期被展開

這里展開在了一個初始化列表中,小技巧

make_index_sequence

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

有兩種形式

  • 編譯器內建
  • 遞歸式的生成

第二種方式嘛還是用到了元編程的慣用伎倆,特化,遞歸式的編程

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

對齊使用也是一樣的形式

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

使用場景

剛才,看見了print去打印的時候打印了 0-(N-1)的元素

那么,這個行為是在編譯期展開的,我們就可以用到其他需要常量的地方

比如一個簡單的需求:

打印tuple

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

exec_for_tuple部分應該非常好懂,但是為什么中間還要再轉發一層呢?

因為tuple元素的個數我們不能直接獲取到,我們寫的又要是一個通用的函數

所以要通過 sizeof...(arg) 這種伎倆來將其元素個數計算出來

如何調用呢?

如下所示:

std::tuple<int, int, double> tuple{1, 2, 3.0};
for_tuple([](auto t) {
  std::cout << t << " ";
}, tuple);

index_sequence_for

那么,看到現在,你知道 index_sequence_for又是何物嗎?

其實吧,剛才就已經見過 index_sequence_for這個東西了

其實就是計算可變長模板參數的個數,然后將其長度做成一個sequence出來

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

結語

index_sequence 是一個不起眼的家伙,可能你平時都不會去了解它,但是這個東西的用途還是有很多的,你想想,編譯器制造一串序列的能力,在tuple這種模板中,使用其是不是更加方便了,在bind這種模板中的參數上,若是使用它,是不是更加靈活好些了。

其實和tuple一個道理,在平常編程中,你也許不會使用它,但是在模板編程中,這是比較好的一個工具。

原文鏈接:https://juejin.cn/post/7160239658272555045

欄目分類
最近更新