From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2181) id 471A2385803F; Thu, 4 Nov 2021 18:33:01 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 471A2385803F MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Jonathan Wakely To: gcc-cvs@gcc.gnu.org, libstdc++-cvs@gcc.gnu.org Subject: [gcc r12-4925] libstdc++: Consolidate duplicate metaprogramming utilities X-Act-Checkin: gcc X-Git-Author: Jonathan Wakely X-Git-Refname: refs/heads/master X-Git-Oldrev: 09aab7e699dcbd79fd64959cf259567bdca94022 X-Git-Newrev: b57899f30f4325a6fe4c791cf01a6a8c94b4ae50 Message-Id: <20211104183301.471A2385803F@sourceware.org> Date: Thu, 4 Nov 2021 18:33:01 +0000 (GMT) X-BeenThere: libstdc++-cvs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libstdc++-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 04 Nov 2021 18:33:01 -0000 https://gcc.gnu.org/g:b57899f30f4325a6fe4c791cf01a6a8c94b4ae50 commit r12-4925-gb57899f30f4325a6fe4c791cf01a6a8c94b4ae50 Author: Jonathan Wakely Date: Thu Nov 4 11:11:58 2021 +0000 libstdc++: Consolidate duplicate metaprogramming utilities Currently std::variant uses __index_of to find the first occurence of a type in a pack, and __exactly_once to check that there is no other occurrence. We can reuse the __find_uniq_type_in_pack() function for both tasks, and remove the recursive templates used to implement __index_of and __exactly_once. libstdc++-v3/ChangeLog: * include/bits/utility.h (__find_uniq_type_in_pack): Move definition to here, ... * include/std/tuple (__find_uniq_type_in_pack): ... from here. * include/std/variant (__detail__variant::__index_of): Remove. (__detail::__variant::__exactly_once): Define using __find_uniq_type_in_pack instead of __index_of. (get, get_if, variant::__index_of): Likewise. Diff: --- libstdc++-v3/include/bits/utility.h | 22 ++++++++++++ libstdc++-v3/include/std/tuple | 22 ------------ libstdc++-v3/include/std/variant | 69 ++++++++++++++----------------------- 3 files changed, 47 insertions(+), 66 deletions(-) diff --git a/libstdc++-v3/include/bits/utility.h b/libstdc++-v3/include/bits/utility.h index c9ffa008217..ec5ed04990b 100644 --- a/libstdc++-v3/include/bits/utility.h +++ b/libstdc++-v3/include/bits/utility.h @@ -102,6 +102,28 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION }; #if __cplusplus >= 201402L + + // Return the index of _Tp in _Types, if it occurs exactly once. + // Otherwise, return sizeof...(_Types). + template + constexpr size_t + __find_uniq_type_in_pack() + { + constexpr size_t __sz = sizeof...(_Types); + constexpr bool __found[__sz] = { __is_same(_Tp, _Types) ... }; + size_t __n = __sz; + for (size_t __i = 0; __i < __sz; ++__i) + { + if (__found[__i]) + { + if (__n < __sz) // more than one _Tp found + return __sz; + __n = __i; + } + } + return __n; + } + // The standard says this macro and alias template should be in but we // we define them here, to be available in , and too. // _GLIBCXX_RESOLVE_LIB_DEFECTS diff --git a/libstdc++-v3/include/std/tuple b/libstdc++-v3/include/std/tuple index b82cdf12569..46173935b64 100644 --- a/libstdc++-v3/include/std/tuple +++ b/libstdc++-v3/include/std/tuple @@ -1419,28 +1419,6 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION #define __cpp_lib_tuples_by_type 201304L - // Return the index of _Tp in _Types, if it occurs exactly once. - // Otherwise, return sizeof...(_Types). - // TODO reuse this for __detail::__variant::__exactly_once. - template - constexpr size_t - __find_uniq_type_in_pack() - { - constexpr size_t __sz = sizeof...(_Types); - constexpr bool __found[__sz] = { __is_same(_Tp, _Types) ... }; - size_t __n = __sz; - for (size_t __i = 0; __i < __sz; ++__i) - { - if (__found[__i]) - { - if (__n < __sz) // more than one _Tp found - return __sz; - __n = __i; - } - } - return __n; - } - /// Return a reference to the unique element of type _Tp of a tuple. template constexpr _Tp& diff --git a/libstdc++-v3/include/std/variant b/libstdc++-v3/include/std/variant index 993ce3dba91..ab4503bc7c1 100644 --- a/libstdc++-v3/include/std/variant +++ b/libstdc++-v3/include/std/variant @@ -161,19 +161,6 @@ namespace __detail { namespace __variant { - // Returns the first appearance of _Tp in _Types. - // Returns sizeof...(_Types) if _Tp is not in _Types. - template - struct __index_of : std::integral_constant {}; - - template - inline constexpr size_t __index_of_v = __index_of<_Tp, _Types...>::value; - - template - struct __index_of<_Tp, _First, _Rest...> : - std::integral_constant - ? 0 : __index_of_v<_Tp, _Rest...> + 1> {}; - // used for raw visitation struct __variant_cookie {}; // used for raw visitation with indices passed in @@ -766,21 +753,9 @@ namespace __variant _Variant_base& operator=(_Variant_base&&) = default; }; - // How many times does _Tp appear in _Types? - template - inline constexpr size_t __count = 0; - - template - inline constexpr size_t __count<_Tp, _Up, _Types...> - = __count<_Tp, _Types...>; - - template - inline constexpr size_t __count<_Tp, _Tp, _Types...> - = 1 + __count<_Tp, _Types...>; - - // TODO: Reuse this in ? template - inline constexpr bool __exactly_once = __count<_Tp, _Types...> == 1; + inline constexpr bool __exactly_once + = std::__find_uniq_type_in_pack<_Tp, _Types...>() < sizeof...(_Types); // Helper used to check for valid conversions that don't involve narrowing. template struct _Arr { _Ti _M_x[1]; }; @@ -1139,45 +1114,51 @@ namespace __variant { static_assert(__detail::__variant::__exactly_once<_Tp, _Types...>, "T must occur exactly once in alternatives"); - return __v.index() == __detail::__variant::__index_of_v<_Tp, _Types...>; + return __v.index() == std::__find_uniq_type_in_pack<_Tp, _Types...>(); } template - constexpr _Tp& get(variant<_Types...>& __v) + constexpr _Tp& + get(variant<_Types...>& __v) { static_assert(__detail::__variant::__exactly_once<_Tp, _Types...>, "T must occur exactly once in alternatives"); static_assert(!is_void_v<_Tp>, "_Tp must not be void"); - return std::get<__detail::__variant::__index_of_v<_Tp, _Types...>>(__v); + constexpr size_t __n = std::__find_uniq_type_in_pack<_Tp, _Types...>(); + return std::get<__n>(__v); } template - constexpr _Tp&& get(variant<_Types...>&& __v) + constexpr _Tp&& + get(variant<_Types...>&& __v) { static_assert(__detail::__variant::__exactly_once<_Tp, _Types...>, "T must occur exactly once in alternatives"); static_assert(!is_void_v<_Tp>, "_Tp must not be void"); - return std::get<__detail::__variant::__index_of_v<_Tp, _Types...>>( - std::move(__v)); + constexpr size_t __n = std::__find_uniq_type_in_pack<_Tp, _Types...>(); + return std::get<__n>(std::move(__v)); } template - constexpr const _Tp& get(const variant<_Types...>& __v) + constexpr const _Tp& + get(const variant<_Types...>& __v) { static_assert(__detail::__variant::__exactly_once<_Tp, _Types...>, "T must occur exactly once in alternatives"); static_assert(!is_void_v<_Tp>, "_Tp must not be void"); - return std::get<__detail::__variant::__index_of_v<_Tp, _Types...>>(__v); + constexpr size_t __n = std::__find_uniq_type_in_pack<_Tp, _Types...>(); + return std::get<__n>(__v); } template - constexpr const _Tp&& get(const variant<_Types...>&& __v) + constexpr const _Tp&& + get(const variant<_Types...>&& __v) { static_assert(__detail::__variant::__exactly_once<_Tp, _Types...>, "T must occur exactly once in alternatives"); static_assert(!is_void_v<_Tp>, "_Tp must not be void"); - return std::get<__detail::__variant::__index_of_v<_Tp, _Types...>>( - std::move(__v)); + constexpr size_t __n = std::__find_uniq_type_in_pack<_Tp, _Types...>(); + return std::get<__n>(std::move(__v)); } template @@ -1214,8 +1195,8 @@ namespace __variant static_assert(__detail::__variant::__exactly_once<_Tp, _Types...>, "T must occur exactly once in alternatives"); static_assert(!is_void_v<_Tp>, "_Tp must not be void"); - return std::get_if<__detail::__variant::__index_of_v<_Tp, _Types...>>( - __ptr); + constexpr size_t __n = std::__find_uniq_type_in_pack<_Tp, _Types...>(); + return std::get_if<__n>(__ptr); } template @@ -1225,8 +1206,8 @@ namespace __variant static_assert(__detail::__variant::__exactly_once<_Tp, _Types...>, "T must occur exactly once in alternatives"); static_assert(!is_void_v<_Tp>, "_Tp must not be void"); - return std::get_if<__detail::__variant::__index_of_v<_Tp, _Types...>>( - __ptr); + constexpr size_t __n = std::__find_uniq_type_in_pack<_Tp, _Types...>(); + return std::get_if<__n>(__ptr); } struct monostate { }; @@ -1402,8 +1383,8 @@ namespace __variant using __accepted_type = __to_type<__accepted_index<_Tp>>; template - static constexpr size_t __index_of = - __detail::__variant::__index_of_v<_Tp, _Types...>; + static constexpr size_t __index_of + = std::__find_uniq_type_in_pack<_Tp, _Types...>(); using _Traits = __detail::__variant::_Traits<_Types...>;