std::experi...al::to_array(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

πŸ’– 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 the std::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 a std::array object 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