diff --git a/include/xo/unit/quantity.hpp b/include/xo/unit/quantity.hpp index c49ab4d5..0df8ec74 100644 --- a/include/xo/unit/quantity.hpp +++ b/include/xo/unit/quantity.hpp @@ -440,6 +440,61 @@ namespace xo { } ///@} + /** @defgroup quantity-comparisonsupport **/ + ///@{ + /** @brief compare this quantity with another, return 3-way comparison + * + * @pre arguments must be quantities having the same dimension + * + * @param y rhs quantity to compare + * @return signed integer; {-ve, 0, +ve} when @c *this is {less than, equal, greater than} @p y + **/ + template + requires quantity_concept && same_dimension_v + auto compare(Quantity2 y) const { + /* convert y to same {units, repr} as *this */ + quantity y2 = y; + + auto cmp = (this->scale_ <=> y2.scale()); + + return cmp; + } + ///@} + + /** @defgroup quantity-comparison **/ + ///@{ + /** @brief 3-way comparison of two quantities + * + * @pre arguments must be quantities having the same dimension + * + * @param y rhs quantity to compare + * @return std::partial_ordering + **/ + template + requires quantity_concept && same_dimension_v + auto operator<=>(Quantity2 y) const { + return this->compare(y); + } + + /** @brief compare two quantities for equality + * + * Although compiler generates this (due to presence of 3-way comparison operator), + * it flags ambiguous overload when included alongside .h files from std::distribution. + * Look like ambiguity would need to be resolved by a header change + **/ + template + requires quantity_concept && same_dimension_v + bool operator==(Quantity2 y) const { + return std::is_eq(this->compare(y)); + } + + template + requires quantity_concept && same_dimension_v + bool operator!=(Quantity2 y) const { + return std::is_neq(this->compare(y)); + } + + /** @addtogroup quantity-unit-conversion **/ ///@{ /** @brief convert to quantity with same dimension, different {unit_type, repr_type}