Difference between revisions of "cpp/experimental/to array"
From cppreference.com
< cpp | experimental
(merged into C++20) |
Andreas Krug (Talk | contribs) m (fmt, {{c}}, headers sorted) |
||
Line 1: | Line 1: | ||
− | {{cpp/experimental/title | to_array}} | + | {{cpp/experimental/title|to_array}} |
{{cpp/experimental/lib_extensions_2/navbar}} | {{cpp/experimental/lib_extensions_2/navbar}} | ||
− | {{fmbox | class=noprint | style=font-size: 0.8em | text='''Merged into ISO C++''' The functionality described on this page was merged into the mainline ISO C++ standard as of 7/2019, see {{ltt|cpp/container/array/to_array|std::to_array}} {{mark since c++20}} }} | + | {{fmbox|class=noprint|style=font-size: 0.8em|text='''Merged into ISO C++''' The functionality described on this page was merged into the mainline ISO C++ standard as of 7/2019, see {{ltt|cpp/container/array/to_array|std::to_array}} {{mark since c++20}}}} |
{{dcl begin}} | {{dcl begin}} | ||
− | {{dcl header |experimental/array}} | + | {{dcl header|experimental/array}} |
− | {{dcl| since=libfund_ts_2 | 1= | + | {{dcl|since=libfund_ts_2|1= |
− | template <class T, std::size_t N> | + | template< class T, std::size_t N > |
constexpr std::array<std::remove_cv_t<T>, N> to_array(T (&a)[N]); | constexpr std::array<std::remove_cv_t<T>, N> to_array(T (&a)[N]); | ||
}} | }} | ||
{{dcl end}} | {{dcl end}} | ||
− | Creates a {{lc|std::array}} from the built-in array {{ | + | Creates a {{lc|std::array}} from the built-in array {{|a}}. The elements of the {{lc|std::array}} are copy-initialized from the corresponding element of {{|a}}. |
===Parameters=== | ===Parameters=== | ||
{{par begin}} | {{par begin}} | ||
− | {{par | a | the built-in array to be used to initialize the {{lc|std::array}}}} | + | {{par|a|the built-in array to be used to initialize the {{lc|std::array}}}} |
{{par end}} | {{par end}} | ||
===Return value=== | ===Return value=== | ||
− | An {{lc|std::array}} object whose elements are copy-initialized from the corresponding element of {{ | + | An {{lc|std::array}} object whose elements are copy-initialized from the corresponding element of {{|a}}. |
===Possible implementation=== | ===Possible implementation=== | ||
{{eq fun|1= | {{eq fun|1= | ||
− | namespace detail | + | namespace detail |
− | + | ||
− | + | ||
− | + | ||
{ | { | ||
− | return { {a[I]...} }; | + | |
− | } | + | |
+ | |||
+ | |||
+ | return { {a[I]...} }; | ||
+ | } | ||
} | } | ||
− | template <class T, std::size_t N> | + | template<class T, std::size_t N> |
constexpr std::array<std::remove_cv_t<T>, N> to_array(T (&a)[N]) | constexpr std::array<std::remove_cv_t<T>, N> to_array(T (&a)[N]) | ||
{ | { | ||
Line 40: | Line 41: | ||
===Example=== | ===Example=== | ||
{{example | {{example | ||
− | + | |code= | |
− | + | ||
#include <cassert> | #include <cassert> | ||
+ | |||
#include <experimental/array> | #include <experimental/array> | ||
#include <unistd.h> | #include <unistd.h> | ||
// mkstemp(3) that works | // mkstemp(3) that works | ||
− | template <std::size_t N> | + | template<std::size_t N> |
int tempfd(char const (&tmpl)[N]) | int tempfd(char const (&tmpl)[N]) | ||
{ | { | ||
Line 64: | Line 65: | ||
assert(rt == 0); | assert(rt == 0); | ||
} | } | ||
− | + | |output= | |
− | + | ||
}} | }} | ||
===See also=== | ===See also=== | ||
{{dsc begin}} | {{dsc begin}} | ||
− | {{dsc inc | cpp/experimental/dsc make array}} | + | {{dsc inc|cpp/experimental/dsc make array}} |
{{dsc end}} | {{dsc end}} | ||
{{langlinks|ja|zh}} | {{langlinks|ja|zh}} |
Latest revision as of 06:02, 6 August 2023
Defined in header <experimental/array>
|
||
template< class T, std::size_t N > constexpr std::array<std::remove_cv_t<T>, N> to_array(T (&a)[N]); |
(library fundamentals TS v2) | |
Creates a std::array from the built-in array a. The elements of the std::array are copy-initialized from the corresponding element of a.
Contents |
[edit] Parameters
a | - | the built-in array to be used to initialize the std::array |
[edit] Return value
An std::array object whose elements are copy-initialized from the corresponding element of a.
[edit] Possible implementation
namespace detail { template<class T, std::size_t N, std::size_t... I> constexpr std::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> constexpr std::array<std::remove_cv_t<T>, N> to_array(T (&a)[N]) { return detail::to_array_impl(a, std::make_index_sequence<N>{}); } |
[edit] Example
Run this code
#include <cassert> #include <cstdlib> #include <experimental/array> #include <unistd.h> // mkstemp(3) that works template<std::size_t N> int tempfd(char const (&tmpl)[N]) { auto s = std::experimental::to_array(tmpl); int fd = mkstemp(s.data()); if (fd != -1) unlink(s.data()); return fd; } int main() { int fd = tempfd("/tmp/test.XXXXXX"); int rt = close(fd); assert(rt == 0); }
[edit] See also
(library fundamentals TS v2) |
creates a std::array object whose size and optionally element type are deduced from the arguments (function template) |