Implements parts of P1614, including synth-three-way and three way comparison for std::pair.
Reviewed By: #libc, Quuxplusone, Mordante
Differential Revision: https://reviews.llvm.org/D107721
| `three_way_comparable_with <https://reviews.llvm.org/D103478>`_",None,Ruslan Arutyunyan,|In Progress|
| `[cmp.result] <https://wg21.link/cmp.result>`_,| `compare_three_way_result <https://reviews.llvm.org/D103581>`_,None,Arthur O'Dwyer,|Complete|
| `[expos.only.func] <https://wg21.link/expos.only.func>`_,"| `synth-three-way <https://reviews.llvm.org/D107721>`_
-| `synth-three-way-result <https://reviews.llvm.org/D107721>`_",[cmp.concept],Kent Ross,|In Progress|
+| `synth-three-way-result <https://reviews.llvm.org/D107721>`_",[cmp.concept],Kent Ross,|Complete|
| `[comparisons.three.way] <https://wg21.link/comparisons.three.way>`_,| `compare_three_way <https://reviews.llvm.org/D80899>`_,[cmp.concept],Christopher Di Bella,|In Progress|
| `[cmp.alg] <https://wg21.link/cmp.alg>`_,"| `strong_order <https://reviews.llvm.org/D107036>`_
| `weak_order <https://reviews.llvm.org/D107036>`_
| `partial_order <https://reviews.llvm.org/D107036>`_",None,Arthur O'Dwyer,|In Progress|
| `[alg.three.way] <https://wg21.link/alg.three.way>`_,| `lexicographical_compare_three_way <https://reviews.llvm.org/D80902>`_,[comparisons.three.way],Christopher Di Bella,|In Progress|
| `[coroutine.handle.compare] <https://wg21.link/coroutine.handle.compare>`_,| coroutine_handle,[comparisons.three.way],Unassigned,|Not Started|
-| `[pairs.spec] <https://wg21.link/pairs.spec>`_,| `pair <https://reviews.llvm.org/D107721>`_,[expos.only.func],Kent Ross,|In Progress|
+| `[pairs.spec] <https://wg21.link/pairs.spec>`_,| `pair <https://reviews.llvm.org/D107721>`_,[expos.only.func],Kent Ross,|Complete|
| `[syserr.errcat.nonvirtuals] <https://wg21.link/syserr.errcat.nonvirtuals>`_,| error_category,[comparisons.three.way],Unassigned,|Not Started|
| `[syserr.compare] <https://wg21.link/syserr.compare>`_,"| error_code
| error_condition",None,Unassigned,|Not Started|
__compare/common_comparison_category.h
__compare/compare_three_way_result.h
__compare/ordering.h
+ __compare/synth_three_way.h
__compare/three_way_comparable.h
__concepts/arithmetic.h
__concepts/assignable.h
--- /dev/null
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef _LIBCPP___COMPARE_SYNTH_THREE_WAY_H
+#define _LIBCPP___COMPARE_SYNTH_THREE_WAY_H
+
+#include <__config>
+#include <__compare/ordering.h>
+#include <__compare/three_way_comparable.h>
+#include <__concepts/boolean_testable.h>
+#include <__utility/declval.h>
+
+#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
+#pragma GCC system_header
+#endif
+
+_LIBCPP_BEGIN_NAMESPACE_STD
+
+#if _LIBCPP_STD_VER > 17 && !defined(_LIBCPP_HAS_NO_SPACESHIP_OPERATOR) && !defined(_LIBCPP_HAS_NO_CONCEPTS)
+
+// [expos.only.func]
+
+_LIBCPP_HIDE_FROM_ABI inline constexpr auto __synth_three_way =
+ []<class _Tp, class _Up>(const _Tp& __t, const _Up& __u)
+ requires requires {
+ { __t < __u } -> __boolean_testable;
+ { __u < __t } -> __boolean_testable;
+ }
+ {
+ if constexpr (three_way_comparable_with<_Tp, _Up>) {
+ return __t <=> __u;
+ } else {
+ if (__t < __u) return weak_ordering::less;
+ if (__u < __t) return weak_ordering::greater;
+ return weak_ordering::equivalent;
+ }
+ };
+
+template <class _Tp, class _Up = _Tp>
+using __synth_three_way_result = decltype(__synth_three_way(declval<_Tp&>(), declval<_Up&>()));
+
+#endif // _LIBCPP_STD_VER > 17 && !defined(_LIBCPP_HAS_NO_SPACESHIP_OPERATOR) && !defined(_LIBCPP_HAS_NO_CONCEPTS)
+
+_LIBCPP_END_NAMESPACE_STD
+
+#endif // _LIBCPP___COMPARE_SYNTH_THREE_WAY_H
#ifndef _LIBCPP___UTILITY_PAIR_H
#define _LIBCPP___UTILITY_PAIR_H
+#include <__compare/common_comparison_category.h>
+#include <__compare/synth_three_way.h>
#include <__config>
#include <__functional/unwrap_ref.h>
#include <__tuple>
pair(_T1, _T2) -> pair<_T1, _T2>;
#endif
+// [pairs.spec], specialized algorithms
+
template <class _T1, class _T2>
inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
bool
return __x.first == __y.first && __x.second == __y.second;
}
+#if _LIBCPP_STD_VER > 17 && !defined(_LIBCPP_HAS_NO_SPACESHIP_OPERATOR) && !defined(_LIBCPP_HAS_NO_CONCEPTS)
+
+template <class _T1, class _T2>
+_LIBCPP_HIDE_FROM_ABI constexpr
+common_comparison_category_t<
+ __synth_three_way_result<_T1>,
+ __synth_three_way_result<_T2> >
+operator<=>(const pair<_T1,_T2>& __x, const pair<_T1,_T2>& __y)
+{
+ if (auto __c = _VSTD::__synth_three_way(__x.first, __y.first); __c != 0) {
+ return __c;
+ }
+ return _VSTD::__synth_three_way(__x.second, __y.second);
+}
+
+#else // _LIBCPP_STD_VER > 17 && !defined(_LIBCPP_HAS_NO_SPACESHIP_OPERATOR) && !defined(_LIBCPP_HAS_NO_CONCEPTS)
+
template <class _T1, class _T2>
inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
bool
return !(__y < __x);
}
+#endif // _LIBCPP_STD_VER > 17 && !defined(_LIBCPP_HAS_NO_SPACESHIP_OPERATOR) && !defined(_LIBCPP_HAS_NO_CONCEPTS)
+
template <class _T1, class _T2>
inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
typename enable_if
module common_comparison_category { private header "__compare/common_comparison_category.h" }
module compare_three_way_result { private header "__compare/compare_three_way_result.h" }
module ordering { private header "__compare/ordering.h" }
+ module synth_three_way { private header "__compare/synth_three_way.h" }
module three_way_comparable { private header "__compare/three_way_comparable.h" }
}
}
};
template <class T1, class T2> bool operator==(const pair<T1,T2>&, const pair<T1,T2>&); // constexpr in C++14
-template <class T1, class T2> bool operator!=(const pair<T1,T2>&, const pair<T1,T2>&); // constexpr in C++14
-template <class T1, class T2> bool operator< (const pair<T1,T2>&, const pair<T1,T2>&); // constexpr in C++14
-template <class T1, class T2> bool operator> (const pair<T1,T2>&, const pair<T1,T2>&); // constexpr in C++14
-template <class T1, class T2> bool operator>=(const pair<T1,T2>&, const pair<T1,T2>&); // constexpr in C++14
-template <class T1, class T2> bool operator<=(const pair<T1,T2>&, const pair<T1,T2>&); // constexpr in C++14
+template <class T1, class T2> bool operator!=(const pair<T1,T2>&, const pair<T1,T2>&); // constexpr in C++14, removed in C++20
+template <class T1, class T2> bool operator< (const pair<T1,T2>&, const pair<T1,T2>&); // constexpr in C++14, removed in C++20
+template <class T1, class T2> bool operator> (const pair<T1,T2>&, const pair<T1,T2>&); // constexpr in C++14, removed in C++20
+template <class T1, class T2> bool operator>=(const pair<T1,T2>&, const pair<T1,T2>&); // constexpr in C++14, removed in C++20
+template <class T1, class T2> bool operator<=(const pair<T1,T2>&, const pair<T1,T2>&); // constexpr in C++14, removed in C++20
+template <class T1, class T2>
+ constexpr common_comparison_type_t<synth-three-way-result<T1>,
+ synth-three-way-result<T2>>
+ operator<=>(const pair<T1,T2>&, const pair<T1,T2>&); // C++20
template <class T1, class T2> pair<V1, V2> make_pair(T1&&, T2&&); // constexpr in C++14
template <class T1, class T2>
--- /dev/null
+// -*- C++ -*-
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// REQUIRES: modules-build
+
+// WARNING: This test was generated by 'generate_private_header_tests.py'
+// and should not be edited manually.
+
+// expected-error@*:* {{use of private header from outside its module: '__compare/synth_three_way.h'}}
+#include <__compare/synth_three_way.h>
--- /dev/null
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// UNSUPPORTED: c++03, c++11, c++14, c++17, libcpp-no-concepts
+// ADDITIONAL_COMPILE_FLAGS: -Wno-sign-compare
+
+// constexpr auto __synth_three_way = ...;
+
+#include <cassert>
+#include <compare>
+#include <limits>
+#include <utility> // Includes synth-three-way via std::pair::operator<=>
+
+#include "test_macros.h"
+
+template <typename T> concept can_synth_three_way = requires(T t) { std::__synth_three_way(t, t); };
+
+// A custom three-way result type
+struct CustomEquality {
+ friend constexpr bool operator==(const CustomEquality&, int) noexcept { return true; }
+ friend constexpr bool operator<(const CustomEquality&, int) noexcept { return false; }
+ friend constexpr bool operator<(int, const CustomEquality&) noexcept { return false; }
+};
+
+constexpr bool test() {
+ {
+ assert(std::__synth_three_way(1, 1) == std::strong_ordering::equal);
+ assert(std::__synth_three_way(2, 1) == std::strong_ordering::greater);
+ assert(std::__synth_three_way(1, 2) == std::strong_ordering::less);
+ ASSERT_SAME_TYPE(std::strong_ordering, std::__synth_three_way_result<int, int>);
+ ASSERT_SAME_TYPE(std::strong_ordering, std::__synth_three_way_result<short, long long int>);
+ }
+ {
+ constexpr double nan = std::numeric_limits<double>::quiet_NaN();
+ assert(std::__synth_three_way(1.0, 1.0) == std::partial_ordering::equivalent);
+ assert(std::__synth_three_way(2.0, 1.0) == std::partial_ordering::greater);
+ assert(std::__synth_three_way(1.0, 2.0) == std::partial_ordering::less);
+ assert(std::__synth_three_way(nan, nan) == std::partial_ordering::unordered);
+ ASSERT_SAME_TYPE(std::partial_ordering, std::__synth_three_way_result<double, double>);
+ ASSERT_SAME_TYPE(std::partial_ordering, std::__synth_three_way_result<double, float>);
+ ASSERT_SAME_TYPE(std::partial_ordering, std::__synth_three_way_result<double, int>);
+ ASSERT_SAME_TYPE(std::partial_ordering, std::__synth_three_way_result<float, short>);
+ }
+ {
+ struct StrongSpaceship {
+ int value;
+ constexpr bool operator==(const StrongSpaceship&) const = default;
+ constexpr std::strong_ordering operator<=>(const StrongSpaceship& other) const { return value <=> other.value; }
+ };
+ assert(std::__synth_three_way(StrongSpaceship{1}, StrongSpaceship{1}) == std::strong_ordering::equal);
+ assert(std::__synth_three_way(StrongSpaceship{2}, StrongSpaceship{1}) == std::strong_ordering::greater);
+ assert(std::__synth_three_way(StrongSpaceship{1}, StrongSpaceship{2}) == std::strong_ordering::less);
+ ASSERT_SAME_TYPE(std::strong_ordering, std::__synth_three_way_result<StrongSpaceship, StrongSpaceship>);
+ }
+ {
+ struct WeakSpaceship {
+ int value;
+ constexpr bool operator==(const WeakSpaceship&) const = default;
+ constexpr std::weak_ordering operator<=>(const WeakSpaceship& other) const {
+ return value <=> other.value;
+ }
+ };
+ assert(std::__synth_three_way(WeakSpaceship{1}, WeakSpaceship{1}) == std::weak_ordering::equivalent);
+ assert(std::__synth_three_way(WeakSpaceship{2}, WeakSpaceship{1}) == std::weak_ordering::greater);
+ assert(std::__synth_three_way(WeakSpaceship{1}, WeakSpaceship{2}) == std::weak_ordering::less);
+ ASSERT_SAME_TYPE(std::weak_ordering, std::__synth_three_way_result<WeakSpaceship, WeakSpaceship>);
+ }
+ {
+ struct PartialSpaceship {
+ double value;
+ constexpr bool operator==(const PartialSpaceship&) const = default;
+ constexpr std::partial_ordering operator<=>(const PartialSpaceship& other) const {
+ return value <=> other.value;
+ }
+ };
+ constexpr double nan = std::numeric_limits<double>::quiet_NaN();
+ assert(std::__synth_three_way(PartialSpaceship{1.0}, PartialSpaceship{1.0}) == std::partial_ordering::equivalent);
+ assert(std::__synth_three_way(PartialSpaceship{2.0}, PartialSpaceship{1.0}) == std::partial_ordering::greater);
+ assert(std::__synth_three_way(PartialSpaceship{1.0}, PartialSpaceship{2.0}) == std::partial_ordering::less);
+ assert(std::__synth_three_way(PartialSpaceship{nan}, PartialSpaceship{nan}) == std::partial_ordering::unordered);
+ ASSERT_SAME_TYPE(std::partial_ordering, std::__synth_three_way_result<PartialSpaceship, PartialSpaceship>);
+ }
+ {
+ struct NoSpaceship {
+ int value;
+ constexpr bool operator==(const NoSpaceship&) const = default;
+ constexpr bool operator<(const NoSpaceship& other) const { return value < other.value; }
+ };
+ assert(std::__synth_three_way(NoSpaceship{1}, NoSpaceship{1}) == std::weak_ordering::equivalent);
+ assert(std::__synth_three_way(NoSpaceship{2}, NoSpaceship{1}) == std::weak_ordering::greater);
+ assert(std::__synth_three_way(NoSpaceship{1}, NoSpaceship{2}) == std::weak_ordering::less);
+ ASSERT_SAME_TYPE(std::weak_ordering, std::__synth_three_way_result<NoSpaceship, NoSpaceship>);
+ }
+ {
+ // Types with operator<=> but no operator== are not three_way_comparable and will fall back to operator< and
+ // compare as weakly ordered.
+ struct SpaceshipNoEquals {
+ constexpr std::strong_ordering operator<=>(const SpaceshipNoEquals&) const {
+ return std::strong_ordering::equivalent;
+ }
+ };
+ assert(std::__synth_three_way(SpaceshipNoEquals{}, SpaceshipNoEquals{}) == std::weak_ordering::equivalent);
+ ASSERT_SAME_TYPE(std::weak_ordering, std::__synth_three_way_result<SpaceshipNoEquals, SpaceshipNoEquals>);
+ }
+ {
+ // Custom three-way-comparison result types cannot satisfy standard concepts (and therefore synth-three-way)
+ // because they are not understood by std::common_comparison_category, but they can still be used in
+ // the same way as standard orderings to do comparisons, and thus can be used by synth-three-way to yield a
+ // weakly-ordered result.
+ struct CustomSpaceship {
+ constexpr CustomEquality operator<=>(const CustomSpaceship&) const { return CustomEquality(); }
+ };
+ assert((CustomSpaceship() <=> CustomSpaceship()) == 0);
+ assert(!(CustomSpaceship() < CustomSpaceship()));
+ assert(std::__synth_three_way(CustomSpaceship(), CustomSpaceship()) == std::weak_ordering::equivalent);
+ ASSERT_SAME_TYPE(std::weak_ordering, std::__synth_three_way_result<CustomSpaceship, CustomSpaceship>);
+ }
+ // SFINAE tests demonstrating synth-three-way needs three_way_comparable or operator<.
+ {
+ struct NoRelative {
+ constexpr bool operator==(const NoRelative&) const;
+ };
+ static_assert(!can_synth_three_way<NoRelative>);
+ }
+ {
+ struct NoLessThan {
+ constexpr bool operator==(const NoLessThan&) const;
+ constexpr bool operator>(const NoLessThan&) const;
+ constexpr bool operator>=(const NoLessThan&) const;
+ constexpr bool operator<=(const NoLessThan&) const;
+ };
+ static_assert(!can_synth_three_way<NoLessThan>);
+ }
+ {
+ assert(std::__synth_three_way(1, 1U) == std::weak_ordering::equivalent);
+ assert(std::__synth_three_way(-1, 0U) == std::weak_ordering::greater);
+ // Even with the warning suppressed (-Wno-sign-compare) there should still be no <=> operator
+ // between signed and unsigned types, so we should end up with a synthesized weak ordering.
+ ASSERT_SAME_TYPE(std::weak_ordering, std::__synth_three_way_result<int, unsigned int>);
+ // When an unsigned type can be narrowed to a larger signed type, <=> should be defined and we
+ // should get a strong ordering. (This probably does not raise a warning due to safe narrowing.)
+ assert((static_cast<long long int>(-1) <=> static_cast<unsigned char>(0)) == std::strong_ordering::less);
+ assert(std::__synth_three_way(static_cast<long long int>(-1),
+ static_cast<unsigned char>(0)) == std::strong_ordering::less);
+ ASSERT_SAME_TYPE(std::strong_ordering, std::__synth_three_way_result<long long int, unsigned char>);
+ }
+
+ return true;
+}
+
+int main(int, char**) {
+ test();
+ static_assert(test());
+
+ {
+ constexpr double nan = std::numeric_limits<double>::quiet_NaN();
+ assert(std::__synth_three_way(nan, 1.0) == std::partial_ordering::unordered);
+ }
+
+ return 0;
+}
--- /dev/null
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// <utility>
+
+// template <class T1, class T2> struct pair
+
+// template <class T1, class T2> bool operator<=>(const pair<T1,T2>&, const pair<T1,T2>&);
+
+// UNSUPPORTED: c++03, c++11, c++14, c++17, libcpp-no-concepts
+
+#include <cassert>
+#include <compare>
+#include <limits>
+#include <type_traits> // std::is_constant_evaluated
+#include <utility>
+
+#include "test_macros.h"
+
+template<class T> concept HasEqual = requires(T t) { t == t; };
+template<class T> concept HasLess = requires(T t) { t < t; };
+template<class T> concept HasSpaceship = requires(T t) { t <=> t; };
+
+constexpr bool test() {
+ {
+ // Pairs of types that both have strong ordering should compare with strong ordering.
+ using P = std::pair<int, int>;
+ ASSERT_SAME_TYPE(decltype(P() <=> P()), std::strong_ordering);
+ assert((P(1, 1) <=> P(1, 2)) == std::strong_ordering::less);
+ assert((P(2, 1) <=> P(1, 2)) == std::strong_ordering::greater);
+ assert((P(0, 0) <=> P(0, 0)) == std::strong_ordering::equal);
+ }
+ {
+ // Pairs of int and a type with no spaceship operator should compare with weak ordering.
+ struct NoSpaceship {
+ int value;
+ constexpr bool operator==(const NoSpaceship&) const = default;
+ constexpr bool operator<(const NoSpaceship& other) const { return value < other.value; }
+ };
+ using P = std::pair<int, NoSpaceship>;
+ ASSERT_SAME_TYPE(decltype(P() <=> P()), std::weak_ordering);
+ assert((P(1, {1}) <=> P(1, {2})) == std::weak_ordering::less);
+ assert((P(2, {1}) <=> P(1, {2})) == std::weak_ordering::greater);
+ assert((P(0, {0}) <=> P(0, {0})) == std::weak_ordering::equivalent);
+ }
+ {
+ // Pairs of int (strongly ordered) and double (partially ordered) should compare with partial ordering.
+ using P = std::pair<int, double>;
+ constexpr double nan = std::numeric_limits<double>::quiet_NaN();
+ ASSERT_SAME_TYPE(decltype(P() <=> P()), std::partial_ordering);
+ assert((P(1, 1.0) <=> P(1, 2.0)) == std::partial_ordering::less);
+ assert((P(1, 1.0) <=> P(1, 1.0)) == std::partial_ordering::equivalent);
+ assert((P(1, -0.0) <=> P(1, 0.0)) == std::partial_ordering::equivalent);
+ assert((P(1, 2.0) <=> P(1, 1.0)) == std::partial_ordering::greater);
+ assert((P(1, nan) <=> P(2, nan)) == std::partial_ordering::less);
+ assert((P(2, nan) <=> P(1, nan)) == std::partial_ordering::greater);
+ assert((P(1, nan) <=> P(1, nan)) == std::partial_ordering::unordered);
+ }
+ {
+ using P = std::pair<double, int>;
+ constexpr double nan = std::numeric_limits<double>::quiet_NaN();
+ ASSERT_SAME_TYPE(decltype(P() <=> P()), std::partial_ordering);
+ assert((P(2.0, 1) <=> P(1.0, 2)) == std::partial_ordering::greater);
+ assert((P(1.0, 1) <=> P(1.0, 2)) == std::partial_ordering::less);
+ assert((P(nan, 1) <=> P(nan, 2)) == std::partial_ordering::unordered);
+ }
+ {
+ struct NoRelative {
+ constexpr bool operator==(const NoRelative&) const;
+ };
+ static_assert(HasEqual<std::pair<int, NoRelative>>);
+ static_assert(!HasLess<std::pair<int, NoRelative>>);
+ static_assert(!HasSpaceship<std::pair<int, NoRelative>>);
+ }
+ {
+ struct NoLessThan {
+ constexpr bool operator==(const NoLessThan&) const;
+ constexpr bool operator>(const NoLessThan&) const;
+ };
+ static_assert(HasEqual<std::pair<int, NoLessThan>>);
+ static_assert(!HasLess<std::pair<int, NoLessThan>>);
+ static_assert(!HasSpaceship<std::pair<int, NoLessThan>>);
+ }
+
+#ifdef TEST_COMPILER_GCC
+ // GCC cannot evaluate NaN @ non-NaN constexpr, so test that runtime-only.
+ if (!std::is_constant_evaluated())
+#endif
+ {
+ {
+ using P = std::pair<int, double>;
+ constexpr double nan = std::numeric_limits<double>::quiet_NaN();
+ assert((P(1, 2.0) <=> P(1, nan)) == std::partial_ordering::unordered);
+ }
+ {
+ using P = std::pair<double, int>;
+ constexpr double nan = std::numeric_limits<double>::quiet_NaN();
+ assert((P(1.0, 1) <=> P(nan, 2)) == std::partial_ordering::unordered);
+ }
+ }
+
+ return true;
+}
+
+int main(int, char**) {
+ test();
+ static_assert(test());
+
+ return 0;
+}