diff options
author | Jonathan Wakely <jwakely@redhat.com> | 2021-11-04 11:11:58 +0000 |
---|---|---|
committer | Jonathan Wakely <jwakely@redhat.com> | 2021-11-04 18:14:50 +0000 |
commit | b57899f30f4325a6fe4c791cf01a6a8c94b4ae50 (patch) | |
tree | 06adad1edae89f6727b2dec5fcaaac7d05ab80ab | |
parent | 09aab7e699dcbd79fd64959cf259567bdca94022 (diff) | |
download | gcc-b57899f30f4325a6fe4c791cf01a6a8c94b4ae50.zip gcc-b57899f30f4325a6fe4c791cf01a6a8c94b4ae50.tar.gz gcc-b57899f30f4325a6fe4c791cf01a6a8c94b4ae50.tar.bz2 |
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.
-rw-r--r-- | libstdc++-v3/include/bits/utility.h | 22 | ||||
-rw-r--r-- | libstdc++-v3/include/std/tuple | 22 | ||||
-rw-r--r-- | 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 c9ffa00..ec5ed04 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 b82cdf1..4617393 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 993ce3d..ab4503b 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...>; |