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

履歴 編集

function template
<array>

std::to_array(C++20)

namespace std {
  template<class T, size_t N>
    constexpr array<remove_cv_t<T>, N> to_array(T (&a)[N]);  // (1)
  template<class T, size_t N>
    constexpr array<remove_cv_t<T>, N> to_array(T (&&a)[N]); // (2)
}

概要

組み込み 1 次元配列から、std::array を作成する。

適格要件

  • (1) : std::is_array_v<T>false かつ is_constructible_v<T, T&>true であること。
  • (2) : std::is_array_v<T>false かつ is_move_constructible_v<T>true であること。

事前条件

  • (1) : TCpp17CopyConstructible 要件を満たしていること。
  • (2) : TCpp17MoveConstructible 要件を満たしていること。

戻り値

  • (1) : {{ a[0], ..., a[N - 1] }}
  • (2) : {{ std::move(a[0]), ..., std::move(a[N - 1]) }}

#include <iostream>
#include <array>
#include <algorithm>
#include <iterator>

int main()
{
  int a[] = { 3, 1, 4, 1, 5 };

  std::array<int, 5> b = std::to_array(a); // 組み込みの配列の要素をコピーし、 std::array を作成

  std::copy(b.begin(), b.end(), std::ostream_iterator<int>(std::cout, ", "));

  int c[3][2] = { { 1, 2 }, { 3, 4 }, { 5, 6 } };

  // auto d = std::to_array(c); // 多次元配列はサポートされない
}

出力

3, 1, 4, 1, 5, 

バージョン

言語

  • C++20

処理系

関連項目

参照