Skip to content

Commit

Permalink
Add spaceship for QuantityPoint
Browse files Browse the repository at this point in the history
  • Loading branch information
chiphogg committed Dec 10, 2024
1 parent 25090bf commit 89c0a5c
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 0 deletions.
1 change: 1 addition & 0 deletions au/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ cc_test(
tags = ["manual"],
deps = [
":quantity",
":quantity_point",
":testing",
":units",
"@com_google_googletest//:gtest_main",
Expand Down
9 changes: 9 additions & 0 deletions au/code/au/cpp20_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
// limitations under the License.

#include "au/quantity.hh"
#include "au/quantity_point.hh"
#include "au/testing.hh"
#include "au/units/meters.hh"

Expand All @@ -26,6 +27,14 @@ struct Foo {
QuantityD<Meters> thickness;
};

struct FooPt {
auto operator<=>(const FooPt &) const = default;

QuantityPointD<Meters> position;
};

TEST(Quantity, SupportsSpaceship) { EXPECT_LT(Foo{5 * m}, Foo{6 * m}); }

TEST(QuantityPoint, SupportsSpaceship) { EXPECT_LT(FooPt{meters_pt(5)}, FooPt{meters_pt(6)}); }

} // namespace au
9 changes: 9 additions & 0 deletions au/code/au/quantity_point.hh
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,15 @@ constexpr auto operator-(QuantityPoint<U1, R1> p1, QuantityPoint<U2, R2> p2) {
return detail::using_common_point_unit(p1, p2, detail::minus);
}

// Spaceship operator provides C++20 compatibility.
#if __cplusplus >= 202002L
template <typename U1, typename R1, typename U2, typename R2>
constexpr auto operator<=>(const QuantityPoint<U1, R1> &lhs, const QuantityPoint<U2, R2> &rhs) {
using U = CommonPointUnitT<U1, U2>;
return lhs.in(U{}) <=> rhs.in(U{});
}
#endif

namespace detail {

template <typename TargetRep, typename U, typename R>
Expand Down

0 comments on commit 89c0a5c

Please sign in to comment.