C++ Logo

std-proposals

Advanced search

[std-proposals] C-style array specialization for std::get

From: Paolo Di Giglio <p.digiglio91_at_[hidden]>
Date: Mon, 28 Feb 2022 17:47:16 +0100
I'd like to propose a specialization of function template std::get for
C-style arrays. A simple two-liner would provide compile-time catching
of out-of-bound array accesses (please note we already have this check
for the std::array specialization):

template <std::size_t Idx, typename T, std::size_t N>
constexpr T& get(T (&arr)[N]) noexcept
{
    static_assert(Idx < N, "Index out of bounds");
    return arr[Idx];
}

As far as I know, it's not possible to pass a C-style array by value to
a function nor to return one from a function. So I think there's no need
for a r-value reference overload.

Thanks in advance for your feedbacks.

Received on 2022-02-28 16:44:37