From 29669521665c5e82a8548ef0a7f642634a042d48 Mon Sep 17 00:00:00 2001 From: Jonathan Wakely Date: Fri, 8 Nov 2019 00:37:08 +0000 Subject: libstdc++: define std::common_comparison_category for C++20 * libsupc++/compare (common_comparison_category) (common_comparison_category_t): Define for C++20. * testsuite/18_support/comparisons/common/1.cc: New test. From-SVN: r277943 --- libstdc++-v3/libsupc++/compare | 99 ++++++++++++++++++++++++++++++++++-------- 1 file changed, 81 insertions(+), 18 deletions(-) (limited to 'libstdc++-v3/libsupc++') diff --git a/libstdc++-v3/libsupc++/compare b/libstdc++-v3/libsupc++/compare index 84cc3f5c85f..94728e29de8 100644 --- a/libstdc++-v3/libsupc++/compare +++ b/libstdc++-v3/libsupc++/compare @@ -385,18 +385,81 @@ namespace std is_gteq(partial_ordering __cmp) noexcept { return __cmp >= 0; } +#if __cpp_lib_concepts + namespace __detail + { + template + inline constexpr unsigned __cmp_cat_id = 1; + template<> + inline constexpr unsigned __cmp_cat_id = 2; + template<> + inline constexpr unsigned __cmp_cat_id = 4; + template<> + inline constexpr unsigned __cmp_cat_id = 8; + + template + constexpr unsigned __cmp_cat_ids() + { return (__cmp_cat_id<_Ts> | ...); } + + template + struct __common_cmp_cat; + + // If any Ti is not a comparison category type, U is void. + template + requires ((_Bits & 1) == 1) + struct __common_cmp_cat<_Bits> { using type = void; }; + + // Otherwise, if at least one Ti is std::partial_ordering, + // U is std::partial_ordering. + template + requires ((_Bits & 0b1001) == 0b1000) + struct __common_cmp_cat<_Bits> { using type = partial_ordering; }; + + // Otherwise, if at least one Ti is std::weak_ordering, + // U is std::weak_ordering. + template + requires ((_Bits & 0b1101) == 0b0100) + struct __common_cmp_cat<_Bits> { using type = weak_ordering; }; + + // Otherwise, U is std::strong_ordering. + template<> + struct __common_cmp_cat<0b0010> { using type = strong_ordering; }; + } // namespace __detail + // [cmp.common], common comparison category type template struct common_comparison_category { - // using type = TODO + using type + = __detail::__common_cmp_cat<__detail::__cmp_cat_ids<_Ts...>()>::type; }; + // Partial specializations for one and zero argument cases. + + template + struct common_comparison_category<_Tp> + { using type = void; }; + + template<> + struct common_comparison_category + { using type = partial_ordering; }; + + template<> + struct common_comparison_category + { using type = weak_ordering; }; + + template<> + struct common_comparison_category + { using type = strong_ordering; }; + + template<> + struct common_comparison_category<> + { using type = strong_ordering; }; + template using common_comparison_category_t = typename common_comparison_category<_Ts...>::type; -#if __cpp_lib_concepts namespace __detail { template @@ -493,22 +556,22 @@ namespace std template requires (three_way_comparable_with<_Tp, _Up> || __detail::__3way_builtin_ptr_cmp<_Tp, _Up>) - constexpr auto - operator()(_Tp&& __t, _Up&& __u) const noexcept - { - if constexpr (__detail::__3way_builtin_ptr_cmp<_Tp, _Up>) - { - auto __pt = static_cast(__t); - auto __pu = static_cast(__u); - if (__builtin_is_constant_evaluated()) - return __pt <=> __pu; - auto __it = reinterpret_cast<__UINTPTR_TYPE__>(__pt); - auto __iu = reinterpret_cast<__UINTPTR_TYPE__>(__pu); - return __it <=> __iu; - } - else - return static_cast<_Tp&&>(__t) <=> static_cast<_Up&&>(__u); - } + constexpr auto + operator()(_Tp&& __t, _Up&& __u) const noexcept + { + if constexpr (__detail::__3way_builtin_ptr_cmp<_Tp, _Up>) + { + auto __pt = static_cast(__t); + auto __pu = static_cast(__u); + if (__builtin_is_constant_evaluated()) + return __pt <=> __pu; + auto __it = reinterpret_cast<__UINTPTR_TYPE__>(__pt); + auto __iu = reinterpret_cast<__UINTPTR_TYPE__>(__pu); + return __it <=> __iu; + } + else + return static_cast<_Tp&&>(__t) <=> static_cast<_Up&&>(__u); + } using is_transparent = void; }; -- cgit v1.2.3