(C++17) | ||||
Sequence | ||||
(C++11) | ||||
(C++26) | ||||
(C++26) | ||||
(C++11) | ||||
Associative | ||||
Unordered associative | ||||
(C++11) | ||||
(C++11) | ||||
(C++11) | ||||
(C++11) | ||||
Adaptors | ||||
(C++23) | ||||
(C++23) | ||||
(C++23) | ||||
(C++23) | ||||
Views | ||||
(C++20) | ||||
(C++23) | ||||
Tables | ||||
Iterator invalidation | ||||
Member function table | ||||
Non-member function table |
Member types | ||||||||||||||||||||||||||
Member functions | ||||||||||||||||||||||||||
Non-member functions | ||||||||||||||||||||||||||
|
| |||||||||||||||||||||||||
Helper classes | ||||||||||||||||||||||||||
Deduction guides(C++17) |
Defined in header <array> | ||
template<class T,std::size_t N> constexprstd::array<std::remove_cv_t<T>, N> to_array( T(&a)[N]); | (1) | (since C++20) |
template<class T,std::size_t N> constexprstd::array<std::remove_cv_t<T>, N> to_array( T(&&a)[N]); | (2) | (since C++20) |
Creates astd::array from the one dimensional built-in arraya
. Copying or moving multidimensional built-in array is not supported.
i
in0, ..., N - 1
, copy-initializes result's correspond element witha[i]. This overload is ill-formed whenstd::is_constructible_v<T, T&> isfalse.i
in0, ..., N - 1
, move-initializes result's correspond element withstd::move(a[i]). This overload is ill-formed whenstd::is_move_constructible_v<T> isfalse.Both overloads are ill-formed whenstd::is_array_v<T> istrue.
Contents |
a | - | the built-in array to be converted thestd::array |
Type requirements | ||
-T must meet the requirements ofCopyConstructible in order to use overload (1). | ||
-T must meet the requirements ofMoveConstructible in order to use overload (2). |
There are some occasions whereclass template argument deduction ofstd::array cannot be used whileto_array
is available:
to_array
can be used when the element type of thestd::array
is manually specified and the length is deduced, which is preferable when implicit conversion is wanted.to_array
can copy a string literal, while class template argument deduction constructs astd::array
of a single pointer to its first character.std::to_array<long>({3,4});// OK: implicit conversion// std::array<long>{3, 4}; // error: too few template argumentsstd::to_array("foo");// creates std::array<char, 4>{'f', 'o', 'o', '\0'}std::array{"foo"};// creates std::array<const char*, 1>{"foo"}
Feature-test macro | Value | Std | Feature |
---|---|---|---|
__cpp_lib_to_array | 201907L | (C++20) | std::to_array |
to_array (1) |
---|
namespace detail{template<class T,std::size_t N,std::size_t...I>constexprstd::array<std::remove_cv_t<T>, N> to_array_impl(T(&a)[N],std::index_sequence<I...>){return{{a[I]...}};}} template<class T,std::size_t N>constexprstd::array<std::remove_cv_t<T>, N> to_array(T(&a)[N]){return detail::to_array_impl(a,std::make_index_sequence<N>{});} |
to_array (2) |
namespace detail{template<class T,std::size_t N,std::size_t...I>constexprstd::array<std::remove_cv_t<T>, N> to_array_impl(T(&&a)[N],std::index_sequence<I...>){return{{std::move(a[I])...}};}} template<class T,std::size_t N>constexprstd::array<std::remove_cv_t<T>, N> to_array(T(&&a)[N]){return detail::to_array_impl(std::move(a),std::make_index_sequence<N>{});} |
#include <array>#include <memory>#include <string_view>#include <type_traits>#include <utility> // creates a constexpr array of string_view'sconstexprauto w1n= std::to_array<std::string_view>({"Mary","Patricia","Linda","Barbara","Elizabeth","Jennifer"});static_assert(std::is_same_v<decltype(w1n),conststd::array<std::string_view,6>>);static_assert(w1n.size()==6 and w1n[5]=="Jennifer"); int main(){// copies a string literalauto a1= std::to_array("foo"); static_assert(a1.size()==4); // deduces both element type and lengthauto a2= std::to_array({0,2,1,3}); static_assert(std::is_same_v<decltype(a2),std::array<int,4>>); // deduces length with element type specified// implicit conversion happensauto a3= std::to_array<long>({0,1,3}); static_assert(std::is_same_v<decltype(a3),std::array<long,3>>); auto a4= std::to_array<std::pair<int,float>>({{3,0.0f},{4,0.1f},{4, 0.1e23f}}); static_assert(a4.size()==3); // creates a non-copyable std::arrayauto a5= std::to_array({std::make_unique<int>(3)}); static_assert(a5.size()==1); // error: copying multidimensional arrays is not supported// char s[2][6] = {"nice", "thing"};// auto a6 = std::to_array(s);}
(library fundamentals TS v2) | creates astd::array object whose size and optionally element type are deduced from the arguments (function template)[edit] |