diff options
author | Jonathan Wakely <jwakely@redhat.com> | 2019-11-08 00:37:08 +0000 |
---|---|---|
committer | Jonathan Wakely <redi@gcc.gnu.org> | 2019-11-08 00:37:08 +0000 |
commit | 29669521665c5e82a8548ef0a7f642634a042d48 (patch) | |
tree | 1de0f458eeb597d38469f4cd353b8e1a119f55a3 /libstdc++-v3/libsupc++ | |
parent | 780bc8922ba77cec3de90ce86eefedf54ad80c8f (diff) |
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
Diffstat (limited to 'libstdc++-v3/libsupc++')
-rw-r--r-- | libstdc++-v3/libsupc++/compare | 99 |
1 files changed, 81 insertions, 18 deletions
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<typename _Tp> + inline constexpr unsigned __cmp_cat_id = 1; + template<> + inline constexpr unsigned __cmp_cat_id<strong_ordering> = 2; + template<> + inline constexpr unsigned __cmp_cat_id<weak_ordering> = 4; + template<> + inline constexpr unsigned __cmp_cat_id<partial_ordering> = 8; + + template<typename... _Ts> + constexpr unsigned __cmp_cat_ids() + { return (__cmp_cat_id<_Ts> | ...); } + + template<unsigned> + struct __common_cmp_cat; + + // If any Ti is not a comparison category type, U is void. + template<unsigned _Bits> + 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<unsigned _Bits> + 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<unsigned _Bits> + 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<typename... _Ts> 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<typename _Tp> + struct common_comparison_category<_Tp> + { using type = void; }; + + template<> + struct common_comparison_category<partial_ordering> + { using type = partial_ordering; }; + + template<> + struct common_comparison_category<weak_ordering> + { using type = weak_ordering; }; + + template<> + struct common_comparison_category<strong_ordering> + { using type = strong_ordering; }; + + template<> + struct common_comparison_category<> + { using type = strong_ordering; }; + template<typename... _Ts> using common_comparison_category_t = typename common_comparison_category<_Ts...>::type; -#if __cpp_lib_concepts namespace __detail { template<typename _Tp, typename _Cat> @@ -493,22 +556,22 @@ namespace std template<typename _Tp, typename _Up> 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<const volatile void*>(__t); - auto __pu = static_cast<const volatile void*>(__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<const volatile void*>(__t); + auto __pu = static_cast<const volatile void*>(__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; }; |