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); // 多次元配列はサポートされない
}
18
#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
処理系
- Clang: 10.0.0 現在未実装 ✅
- GCC: 10.0.0 ✅
- Visual C++: ??