Skip to content

Commit 47fffe6

Browse files
committed
Add equality comparison
1 parent 2532bae commit 47fffe6

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

include/rvarago/refined.hpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@ namespace rvarago::refined {
88

99
// Properties a refinement implements.
1010
struct traits {
11-
// Comparison operations with the spaceship operator.
11+
// Equality comparison (`==`, `!=`).
12+
bool equality{false};
13+
14+
// Comparison operations (`<`, `>`, etc) with the spaceship operator.
1215
bool ordered{false};
1316
};
1417

@@ -64,6 +67,11 @@ class refinement {
6467
return refinement{std::move(value)};
6568
}
6669

70+
friend constexpr auto operator==(refinement const &, refinement const &)
71+
-> bool
72+
requires(Traits.equality)
73+
= default;
74+
6775
friend constexpr auto operator<=>(refinement const &, refinement const &)
6876
requires(Traits.ordered)
6977
= default;

test/refined_test.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,15 @@ TEST_CASE("A to_exception policy should throw on invalid argument",
5959
refined::error::to_exception::refinement_exception);
6060
}
6161

62+
TEST_CASE("A refinement can be equality comparable", "[traits][equality]") {
63+
64+
using ref_cmp = refined::refinement<int, [](auto const &) { return true; },
65+
refined::traits{.equality = true}>;
66+
67+
STATIC_REQUIRE(*ref_cmp::make(1) == *ref_cmp::make(1));
68+
STATIC_REQUIRE(*ref_cmp::make(1) != *ref_cmp::make(2));
69+
}
70+
6271
TEST_CASE("A refinement can be ordered", "[traits][sorted]") {
6372

6473
using ref_cmp = refined::refinement<int, [](auto const &) { return true; },

0 commit comments

Comments
 (0)