Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/experimental/to array"

From cppreference.com
(merged into C++20)
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 {{tt|a}}. The elements of the {{lc|std::array}} are copy-initialized from the corresponding element of {{tt|a}}.
+
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 {{tt|a}}.
+
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
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]...} };
+
    
}
+
 +
 +
 +
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=
+
|code=
#include <cstdlib>
+
 
#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=
| 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

 
 
Experimental
Technical Specification
Filesystem library (filesystem TS)
Library fundamentals (library fundamentals TS)
Library fundamentals 2 (library fundamentals TS v2)
Library fundamentals 3 (library fundamentals TS v3)
Extensions for parallelism (parallelism TS)
Extensions for parallelism 2 (parallelism TS v2)
Extensions for concurrency (concurrency TS)
Extensions for concurrency 2 (concurrency TS v2)
Concepts (concepts TS)
Ranges (ranges TS)
Reflection (reflection TS)
Mathematical special functions (special functions TR)
Experimental Non-TS
Pattern Matching
Linear Algebra
std::execution
Contracts
2D Graphics
 
 
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

#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) [edit]