最終更新日時(UTC):
が更新

履歴 編集

function template
<utility>

std::get(C++26)

namespace std {
  template <std::size_t I, class T, T... Values>
  constexpr T get(integer_sequence<T, Values...>) noexcept; // (1) C++26
}

概要

integer_sequenceから、I番目の整数値を取得する。

適格要件

  • I < sizeof...(Values)であること

戻り値

Values...I番目の値

例外

投げない

基本的な使い方

#include <iostream>
#include <utility>

int main()
{
  using Seq = std::integer_sequence<int, 10, 20, 30>;

  std::cout << std::get<0>(Seq{}) << std::endl;
  std::cout << std::get<1>(Seq{}) << std::endl;
  std::cout << std::get<2>(Seq{}) << std::endl;
}

出力

10
20
30

構造化束縛で使用する

構造化束縛は内部的にget<I>を呼び出して各要素を取り出す。

#include <iostream>
#include <utility>

template <std::size_t Count>
void run() {
  // 各Indexはget<I>(make_index_sequence<Count>{})から取り出される
  constexpr auto [...Index] = std::make_index_sequence<Count>{};
  ((std::cout << Index << ' '), ...);
  std::cout << std::endl;
}

int main()
{
  run<3>();
}

出力

0 1 2 

template for文で使用する

template forはループの各反復でget<I>を呼び出す。

#include <iostream>
#include <utility>

int main()
{
  // 各反復で get<0>, get<1>, get<2> が呼び出される
  template for (auto I : std::make_index_sequence<3>{}) {
    std::cout << I << ' ';
  }
  std::cout << std::endl;
}

出力

0 1 2 

バージョン

言語

  • C++26

処理系

関連項目

参照