GeezerVison is showing cpprefjp.github.io — std::to_array - cpprefjp C++日本語リファレンス
最終更新日時:
2026年09月30日 18時24分32秒 (JST)
Akira Takahashi
が更新
履歴
編集
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) : T は Cpp17CopyConstructible 要件を満たしていること。
(2) : T は Cpp17MoveConstructible 要件を満たしていること。
戻り値
(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,
バージョン
言語
処理系
関連項目
参照