libstdc++: Optimize std::tuple_element and std::tuple_size_v
authorJonathan Wakely <jwakely@redhat.com>
Thu, 4 Nov 2021 09:31:50 +0000 (09:31 +0000)
committerJonathan Wakely <jwakely@redhat.com>
Thu, 4 Nov 2021 18:14:50 +0000 (18:14 +0000)
commit09aab7e699dcbd79fd64959cf259567bdca94022
tree7467ae1110fb0c821837196d25106ab817c28c67
parent1b4a63593bc6b9770789816b205039fdf3cfd3fc
libstdc++: Optimize std::tuple_element and std::tuple_size_v

This reduces the number of class template instantiations needed for code
using tuples, by reusing _Nth_type in tuple_element and specializing
tuple_size_v for tuple, pair and array (and const-qualified versions of
them).

Also define the _Nth_type primary template as a complete type (but with
no nested 'type' member).  This avoids "invalid use of incomplete type"
errors for out-of-range specializations of tuple_element. Those errors
would probably be confusing and unhelpful for users. We already have
a user-friendly static assert in tuple_element itself.

Also ensure that tuple_size_v is available whenever tuple_size is (as
proposed by LWG 3387). We already do that for tuple_element_t.

libstdc++-v3/ChangeLog:

* include/bits/stl_pair.h (tuple_size_v): Define partial
specializations for std::pair.
* include/bits/utility.h (_Nth_type): Move definition here
and define primary template.
(tuple_size_v): Move definition here.
* include/std/array (tuple_size_v): Define partial
specializations for std::array.
* include/std/tuple (tuple_size_v): Move primary template to
<bits/utility.h>.  Define partial specializations for
std::tuple.
(tuple_element): Change definition to use _Nth_type.
* include/std/variant (_Nth_type): Move to <bits/utility.h>.
(variant_alternative, variant): Adjust qualification of
_Nth_type.
* testsuite/20_util/tuple/element_access/get_neg.cc: Prune
additional errors from _Nth_type.
libstdc++-v3/include/bits/stl_pair.h
libstdc++-v3/include/bits/utility.h
libstdc++-v3/include/std/array
libstdc++-v3/include/std/tuple
libstdc++-v3/include/std/variant
libstdc++-v3/testsuite/20_util/tuple/element_access/get_neg.cc