public inbox for libstdc++-cvs@sourceware.org help / color / mirror / Atom feed
From: Jonathan Wakely <redi@gcc.gnu.org> To: gcc-cvs@gcc.gnu.org, libstdc++-cvs@gcc.gnu.org Subject: [gcc r12-4925] libstdc++: Consolidate duplicate metaprogramming utilities Date: Thu, 4 Nov 2021 18:33:01 +0000 (GMT) [thread overview] Message-ID: <20211104183301.471A2385803F@sourceware.org> (raw) https://gcc.gnu.org/g:b57899f30f4325a6fe4c791cf01a6a8c94b4ae50 commit r12-4925-gb57899f30f4325a6fe4c791cf01a6a8c94b4ae50 Author: Jonathan Wakely <jwakely@redhat.com> Date: Thu Nov 4 11:11:58 2021 +0000 libstdc++: Consolidate duplicate metaprogramming utilities Currently std::variant uses __index_of<T, Types...> to find the first occurence of a type in a pack, and __exactly_once<T, Types...> to check that there is no other occurrence. We can reuse the __find_uniq_type_in_pack<T, Types...>() 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<T>, get_if<T>, 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<typename _Tp, typename... _Types> + 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 <tuple> but we // we define them here, to be available in <array>, <utility> and <ranges> 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<typename _Tp, typename... _Types> - 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 <typename _Tp, typename... _Types> 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<typename _Tp, typename... _Types> - struct __index_of : std::integral_constant<size_t, 0> {}; - - template<typename _Tp, typename... _Types> - inline constexpr size_t __index_of_v = __index_of<_Tp, _Types...>::value; - - template<typename _Tp, typename _First, typename... _Rest> - struct __index_of<_Tp, _First, _Rest...> : - std::integral_constant<size_t, is_same_v<_Tp, _First> - ? 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<typename _Tp, typename... _Types> - inline constexpr size_t __count = 0; - - template<typename _Tp, typename _Up, typename... _Types> - inline constexpr size_t __count<_Tp, _Up, _Types...> - = __count<_Tp, _Types...>; - - template<typename _Tp, typename... _Types> - inline constexpr size_t __count<_Tp, _Tp, _Types...> - = 1 + __count<_Tp, _Types...>; - - // TODO: Reuse this in <tuple> ? template<typename _Tp, typename... _Types> - 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<typename _Ti> 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<typename _Tp, typename... _Types> - 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<typename _Tp, typename... _Types> - 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<typename _Tp, typename... _Types> - 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<typename _Tp, typename... _Types> - 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<size_t _Np, typename... _Types> @@ -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<typename _Tp, typename... _Types> @@ -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<typename _Tp> - 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...>;
reply other threads:[~2021-11-04 18:33 UTC|newest] Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions: You may reply publicly to this message via plain-text email using any one of the following methods: * Save the following mbox file, import it into your mail client, and reply-to-all from there: mbox Avoid top-posting and favor interleaved quoting: https://en.wikipedia.org/wiki/Posting_style#Interleaved_style * Reply using the --to, --cc, and --in-reply-to switches of git-send-email(1): git send-email \ --in-reply-to=20211104183301.471A2385803F@sourceware.org \ --to=redi@gcc.gnu.org \ --cc=gcc-cvs@gcc.gnu.org \ --cc=libstdc++-cvs@gcc.gnu.org \ /path/to/YOUR_REPLY https://kernel.org/pub/software/scm/git/docs/git-send-email.html * If your mail client supports setting the In-Reply-To header via mailto: links, try the mailto: linkBe sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox; as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).