-
- Downloads
c++: Define built-in for std::tuple_element [PR100157]
This adds a new built-in to replace the recursive class template
instantiations done by traits such as std::tuple_element and
std::variant_alternative. The purpose is to select the Nth type from a
list of types, e.g. __type_pack_element<1, char, int, float> is int.
We implement it as a special kind of TRAIT_TYPE.
For a pathological example tuple_element_t<1000, tuple<2000 types...>>
the compilation time is reduced by more than 90% and the memory used by
the compiler is reduced by 97%. In realistic examples the gains will be
much smaller, but still relevant.
Unlike the other built-in traits, __type_pack_element uses template-id
syntax instead of call syntax and is SFINAE-enabled, matching Clang's
implementation. And like the other built-in traits, it's not mangleable
so we can't use it directly in function signatures.
N.B. Clang seems to implement __type_pack_element as a first-class
template that can e.g. be used as a template-template argument. For
simplicity we implement it in a more ad-hoc way.
Co-authored-by:
Jonathan Wakely <jwakely@redhat.com>
PR c++/100157
gcc/cp/ChangeLog:
* cp-trait.def (TYPE_PACK_ELEMENT): Define.
* cp-tree.h (finish_trait_type): Add complain parameter.
* cxx-pretty-print.cc (pp_cxx_trait): Handle
CPTK_TYPE_PACK_ELEMENT.
* parser.cc (cp_parser_constant_expression): Document default
arguments.
(cp_parser_trait): Handle CPTK_TYPE_PACK_ELEMENT. Pass
tf_warning_or_error to finish_trait_type.
* pt.cc (tsubst) <case TRAIT_TYPE>: Handle non-type first
argument. Pass complain to finish_trait_type.
* semantics.cc (finish_type_pack_element): Define.
(finish_trait_type): Add complain parameter. Handle
CPTK_TYPE_PACK_ELEMENT.
* tree.cc (strip_typedefs): Handle non-type first argument.
Pass tf_warning_or_error to finish_trait_type.
* typeck.cc (structural_comptypes) <case TRAIT_TYPE>: Use
cp_tree_equal instead of same_type_p for the first argument.
libstdc++-v3/ChangeLog:
* include/bits/utility.h (_Nth_type): Conditionally define in
terms of __type_pack_element if available.
* testsuite/20_util/tuple/element_access/get_neg.cc: Prune
additional errors from the new built-in.
gcc/testsuite/ChangeLog:
* g++.dg/ext/type_pack_element1.C: New test.
* g++.dg/ext/type_pack_element2.C: New test.
* g++.dg/ext/type_pack_element3.C: New test.
Showing
- gcc/cp/cp-trait.def 1 addition, 0 deletionsgcc/cp/cp-trait.def
- gcc/cp/cp-tree.h 1 addition, 1 deletiongcc/cp/cp-tree.h
- gcc/cp/cxx-pretty-print.cc 16 additions, 5 deletionsgcc/cp/cxx-pretty-print.cc
- gcc/cp/parser.cc 37 additions, 7 deletionsgcc/cp/parser.cc
- gcc/cp/pt.cc 6 additions, 2 deletionsgcc/cp/pt.cc
- gcc/cp/semantics.cc 38 additions, 1 deletiongcc/cp/semantics.cc
- gcc/cp/tree.cc 7 additions, 3 deletionsgcc/cp/tree.cc
- gcc/cp/typeck.cc 1 addition, 1 deletiongcc/cp/typeck.cc
- gcc/testsuite/g++.dg/ext/type_pack_element1.C 19 additions, 0 deletionsgcc/testsuite/g++.dg/ext/type_pack_element1.C
- gcc/testsuite/g++.dg/ext/type_pack_element2.C 14 additions, 0 deletionsgcc/testsuite/g++.dg/ext/type_pack_element2.C
- gcc/testsuite/g++.dg/ext/type_pack_element3.C 22 additions, 0 deletionsgcc/testsuite/g++.dg/ext/type_pack_element3.C
- libstdc++-v3/include/bits/utility.h 6 additions, 0 deletionslibstdc++-v3/include/bits/utility.h
- libstdc++-v3/testsuite/20_util/tuple/element_access/get_neg.cc 1 addition, 0 deletions...dc++-v3/testsuite/20_util/tuple/element_access/get_neg.cc
Loading
Please register or sign in to comment