]> git.ipfire.org Git - thirdparty/gcc.git/commit - libstdc++-v3/include/std/array
libstdc++: Simplify std::array accessors [PR104719]
authorJonathan Wakely <jwakely@redhat.com>
Fri, 25 Mar 2022 10:28:28 +0000 (10:28 +0000)
committerJonathan Wakely <jwakely@redhat.com>
Wed, 4 May 2022 15:24:56 +0000 (16:24 +0100)
commitef8d5ac08b5e60f35c52087d88c0235c8ce6b65b
tree3fc9436691a09e050ad8890048b56c23c2a751bf
parent9c6a4beeed572f9e235f881e00ad8c63b6bcc9df
libstdc++: Simplify std::array accessors [PR104719]

This removes the __array_traits::_S_ref and __array_traits::_S_ptr
accessors, which only exist to make the special case of std::array<T, 0>
syntactically well-formed.

By changing the empty type used as the std::array<T, 0>::_M_elems data
member to support operator[] and conversion to a pointer, we can write
code using the natural syntax. The indirection through _S_ref and
_S_ptr is removed for the common case, and a function call is only used
for the special case of zero-size arrays.

The invalid member access for zero-sized arrays is changed to use
__builtin_trap() instead of a null dereference. This guarantees a
runtime error if it ever gets called, instead of undefined behaviour
that is likely to get optimized out as unreachable.

libstdc++-v3/ChangeLog:

PR libstdc++/104719
* include/std/array (__array_traits::_S_ref): Remove.
(__array_traits::_S_ptr): Remove.
(__array_traits<T, 0>::_Type): Define operator[] and operator T*
to provide an array-like API.
(array::_AT_Type): Remove public typeef.
(array::operator[], array::at, array::front, array::back): Use
index operator to access _M_elems instead of _S_ref.
(array::data): Use implicit conversion from _M_elems to pointer.
(swap(array&, array&)): Use __enable_if_t helper.
(get<I>): Use index operator to access _M_elems.
* testsuite/23_containers/array/tuple_interface/get_neg.cc:
Adjust dg-error line numbers.
libstdc++-v3/include/std/array
libstdc++-v3/testsuite/23_containers/array/tuple_interface/get_neg.cc