Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement P2167R3 Improving boolean-testable Usage (for comparison fallback CPOs) #3258

Merged
merged 3 commits into from
Dec 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions stl/inc/compare
Original file line number Diff line number Diff line change
Expand Up @@ -641,8 +641,8 @@ inline namespace _Cpos {

template <class _Ty1, class _Ty2>
concept _Can_fallback_eq_lt = requires(_Ty1& _Left, _Ty2& _Right) {
{ _Left == _Right } -> _Implicitly_convertible_to<bool>;
{ _Left < _Right } -> _Implicitly_convertible_to<bool>;
{ _Left == _Right } -> _Boolean_testable;
{ _Left < _Right } -> _Boolean_testable;
};

template <class _Ty1, class _Ty2>
Expand Down Expand Up @@ -751,9 +751,9 @@ concept _Can_partial_order = requires(_Ty1& _Left, _Ty2& _Right) { _STD partial_
namespace _Compare_partial_order_fallback {
template <class _Ty1, class _Ty2>
concept _Can_fallback_eq_lt_twice = requires(_Ty1& _Left, _Ty2& _Right) {
{ _Left == _Right } -> _Implicitly_convertible_to<bool>;
{ _Left < _Right } -> _Implicitly_convertible_to<bool>;
{ _Right < _Left } -> _Implicitly_convertible_to<bool>;
{ _Left == _Right } -> _Boolean_testable;
{ _Left < _Right } -> _Boolean_testable;
{ _Right < _Left } -> _Boolean_testable;
};

class _Cpo {
Expand Down
1 change: 1 addition & 0 deletions stl/inc/yvals_core.h
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,7 @@
// P2102R0 Making "Implicit Expression Variations" More Explicit
// P2106R0 Range Algorithm Result Types
// P2116R0 Removing tuple-Like Protocol Support From Fixed-Extent span
// P2167R3 Improving boolean-testable Usage
// P2210R2 Superior String Splitting
// P2216R3 std::format Improvements
// P2231R1 Completing constexpr In optional And variant
Expand Down
59 changes: 59 additions & 0 deletions tests/std/tests/P0768R1_spaceship_cpos/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,65 @@ namespace {
static_assert(is_same_v<CpoResult<compare_partial_order_fallback, F<13>, const F<13>>, Partial>); // [cmp.alg]/6.3
} // namespace

// Test strengthened requirements in P2167R3: compare_*_order_fallback CPOs require return types to be boolean-testable.
enum class ResultKind : bool {
Bad,
Good,
};

template <ResultKind K>
struct ComparisonResult {
bool value;

constexpr operator bool() const noexcept {
return value;
}

constexpr auto operator!() const noexcept {
if constexpr (K == ResultKind::Good) {
return ComparisonResult{!value};
}
}
};

template <ResultKind EqKind, ResultKind LeKind>
struct BoolTestType {
friend constexpr ComparisonResult<EqKind> operator==(BoolTestType, BoolTestType) noexcept {
return ComparisonResult<EqKind>{true};
}

friend constexpr ComparisonResult<LeKind> operator<(BoolTestType, BoolTestType) noexcept {
return ComparisonResult<LeKind>{false};
}
};

static_assert(is_same_v<CpoResult<compare_strong_order_fallback, BoolTestType<ResultKind::Bad, ResultKind::Bad>>,
IllFormed>); // [cmp.alg]/4.3
static_assert(is_same_v<CpoResult<compare_strong_order_fallback, BoolTestType<ResultKind::Bad, ResultKind::Good>>,
IllFormed>); // [cmp.alg]/4.3
static_assert(is_same_v<CpoResult<compare_strong_order_fallback, BoolTestType<ResultKind::Good, ResultKind::Bad>>,
IllFormed>); // [cmp.alg]/4.3
static_assert(is_same_v<CpoResult<compare_strong_order_fallback, BoolTestType<ResultKind::Good, ResultKind::Good>>,
strong_ordering>); // [cmp.alg]/4.3

static_assert(is_same_v<CpoResult<compare_weak_order_fallback, BoolTestType<ResultKind::Bad, ResultKind::Bad>>,
IllFormed>); // [cmp.alg]/5.3
static_assert(is_same_v<CpoResult<compare_weak_order_fallback, BoolTestType<ResultKind::Bad, ResultKind::Good>>,
IllFormed>); // [cmp.alg]/5.3
static_assert(is_same_v<CpoResult<compare_weak_order_fallback, BoolTestType<ResultKind::Good, ResultKind::Bad>>,
IllFormed>); // [cmp.alg]/5.3
static_assert(is_same_v<CpoResult<compare_weak_order_fallback, BoolTestType<ResultKind::Good, ResultKind::Good>>,
weak_ordering>); // [cmp.alg]/5.3

static_assert(is_same_v<CpoResult<compare_partial_order_fallback, BoolTestType<ResultKind::Bad, ResultKind::Bad>>,
IllFormed>); // [cmp.alg]/6.3
static_assert(is_same_v<CpoResult<compare_partial_order_fallback, BoolTestType<ResultKind::Bad, ResultKind::Good>>,
IllFormed>); // [cmp.alg]/6.3
static_assert(is_same_v<CpoResult<compare_partial_order_fallback, BoolTestType<ResultKind::Good, ResultKind::Bad>>,
IllFormed>); // [cmp.alg]/6.3
static_assert(is_same_v<CpoResult<compare_partial_order_fallback, BoolTestType<ResultKind::Good, ResultKind::Good>>,
partial_ordering>); // [cmp.alg]/6.3

// Test when the type is comparable through ADL. Part B, exception specifications.
static_assert(!NoexceptCpo<std::strong_order, TestAdl::StrongType<Throwing>>); // [cmp.alg]/1.2
static_assert(!NoexceptCpo<std::weak_order, TestAdl::WeakType<Throwing>>); // [cmp.alg]/2.2
Expand Down