std::experi...al::to_array(3)
π NAME π
std::experimental::to_array β std::experimental::to_array. Yep, bestie, the name is literally the same on both sides, no notes. This is the std::experi...al::to_array(3) page, and the manual name folded in near the top is C++ Standard Libary.
β¨ Synopsis β¨
Okay so this is defined in the header <experimental/array>, and the synopsis is giving βI got line-wrapped in the group chatβ energy. Hereβs the exact text as it landed:
Defined in header <experimental/array>
template< class T, std::size_t N > (library
fundamentals TS
constexpr std::array<std::remove_cv_t<T>, N> to_array(T v2)
(&a)[N]);
In human-readable shape thatβs constexpr std::array<std::remove_cv_t<T>, N> to_array(T (&a)[N]);, and itβs marked as library fundamentals TS. What it does: creates a std::array from the built-in array a, and the elements of the std::array are copy-initialized from the corresponding element of a. Periodt. π
πΈ Parameters πΈ
aβ the built-in array that gets used to initialize thestd::array.
π¦ Return value π¦
It returns an std::array object whose elements are copy-initialized from the corresponding element of a. Cute, useful, consistent. π«Ά
π§ Possible implementation π§
If you wanna peek under the hood, hereβs the possible implementation, copied exactly:
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>{});
}
π Example π
This example is a mkstemp(3) that works, and honestly it ate:
// 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);
}
π See also π
make_arrayβ creates astd::arrayobject whose size and optionally element type are deduced from the arguments (function template) (library fundamentals TS v2). Obsessed with the deduction, fr fr.
okay bestie thatβs the whole page β source http://cppreference.com, version/date 2024.06.10, title std::experi...al::to_array(3). byeee π
β¨
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms