Skip to content

Commit

Permalink
feat: zero limited only to comparisons and initialization of quanti…
Browse files Browse the repository at this point in the history
…ties
  • Loading branch information
mpusz committed Sep 1, 2023
1 parent 3e972b9 commit 0dd28c1
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 52 deletions.
7 changes: 3 additions & 4 deletions docs/users_guide/framework_basics/the_affine_space.md
Original file line number Diff line number Diff line change
Expand Up @@ -235,10 +235,9 @@ Taxi distance: 31.2544 km

!!! note

Subtracting two point origins defined in terms of `absolute_point_origin`
(i.e. `mean_sea_level - mean_sea_level`) results with `zero`. The reason for this is the fact
that those origins do not contain any information about the unit so we are not able to determine
more explicit resulting `quantity` type.
It is not allowed to subtract two point origins defined in terms of `absolute_point_origin`
(i.e. `mean_sea_level - mean_sea_level`) as those do not contain information about the unit
so we are not able to determine a resulting `quantity` type.


### Temperature support
Expand Down
27 changes: 0 additions & 27 deletions src/core/include/mp-units/quantity.h
Original file line number Diff line number Diff line change
Expand Up @@ -275,8 +275,6 @@ class quantity {
return *this;
}

constexpr quantity& operator%=(zero_t) = delete;

template<typename Value>
requires(!Quantity<Value>) && requires(rep a, const Value b) {
{
Expand Down Expand Up @@ -440,31 +438,6 @@ template<auto R1, typename Rep1, auto R2, typename Rep2>
return ct(lhs).numerical_value() <=> ct(rhs).numerical_value();
}


template<auto R, typename Rep>
[[nodiscard]] constexpr Quantity auto operator+(const quantity<R, Rep>& q, zero_t)
{
return q;
}

template<auto R, typename Rep>
[[nodiscard]] constexpr Quantity auto operator+(zero_t, const quantity<R, Rep>& q)
{
return q;
}

template<auto R, typename Rep>
[[nodiscard]] constexpr Quantity auto operator-(const quantity<R, Rep>& q, zero_t)
{
return q;
}

template<auto R, typename Rep>
[[nodiscard]] constexpr Quantity auto operator-(zero_t, const quantity<R, Rep>& q)
{
return -q;
}

template<auto R, typename Rep>
[[nodiscard]] constexpr bool operator==(const quantity<R, Rep>& q, zero_t)
{
Expand Down
7 changes: 0 additions & 7 deletions src/core/include/mp-units/quantity_point.h
Original file line number Diff line number Diff line change
Expand Up @@ -323,13 +323,6 @@ template<PointOrigin PO, QuantityPointOf<PO{}> QP>
return -(qp - po);
}

template<PointOrigin PO>
requires detail::is_derived_from_specialization_of_absolute_point_origin<PO>
[[nodiscard]] constexpr zero_t operator-(PO, PO)
{
return {};
}

template<PointOrigin PO1, detail::SameAbsolutePointOriginAs<PO1{}> PO2>
requires QuantitySpecOf<std::remove_const_t<decltype(PO1::quantity_spec)>, PO2::quantity_spec> &&
(detail::is_derived_from_specialization_of_relative_point_origin<PO1> ||
Expand Down
4 changes: 3 additions & 1 deletion test/unit_test/static/quantity_point_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -736,6 +736,9 @@ concept invalid_binary_operations = requires {
// cant'subtract two unrelated points
requires !requires { Origin - OtherOrigin; };

// cant'subtract the same point as we do not know the unit for the resulting quantity
requires !requires { Origin - Origin; };

// unit constants
requires !requires { QP<si::metre, mean_sea_level, int>(1) + m; };
requires !requires { QP<si::metre, mean_sea_level, int>(1) - m; };
Expand Down Expand Up @@ -918,7 +921,6 @@ static_assert(is_of_type<mean_sea_level - ground_level, quantity<isq::height[m],
static_assert(is_of_type<ground_level - mean_sea_level, quantity<isq::height[m], int>>);
static_assert(is_of_type<ground_level - tower_peak, quantity<isq::height[m], int>>);
static_assert(is_of_type<tower_peak - ground_level, quantity<isq::height[m], int>>);
static_assert(is_of_type<mean_sea_level - mean_sea_level, zero_t>);

static_assert(is_of_type<(1 * m + mean_sea_level) - (1 * m + ground_level), quantity<isq::height[m], int>>);
static_assert(is_of_type<(1 * m + ground_level) - (1 * m + mean_sea_level), quantity<isq::height[m], int>>);
Expand Down
17 changes: 4 additions & 13 deletions test/unit_test/static/quantity_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -445,11 +445,6 @@ concept invalid_compound_assignments = requires() {
requires !requires(Q<isq::length[m], int> l) { l *= m; };
requires !requires(Q<isq::length[m], int> l) { l /= m; };
requires !requires(Q<isq::length[m], int> l) { l %= m; };

// zero
requires !requires(Q<isq::length[m], int> l) { l *= zero; };
requires !requires(Q<isq::length[m], int> l) { l /= zero; };
requires !requires(Q<isq::length[m], int> l) { l %= zero; };
};
static_assert(invalid_compound_assignments<quantity>);

Expand Down Expand Up @@ -481,6 +476,10 @@ concept invalid_binary_operations = requires {
requires !requires { m % Q<isq::length[m], int>(1); };

// zero
requires !requires(Q<isq::length[m], double> a) { a + zero; };
requires !requires(Q<isq::length[m], double> a) { zero + a; };
requires !requires(Q<isq::length[m], double> a) { a - zero; };
requires !requires(Q<isq::length[m], double> a) { zero - a; };
requires !requires(Q<isq::length[m], double> a) { a* zero; };
requires !requires(Q<isq::length[m], double> a) { zero* a; };
requires !requires(Q<isq::length[m], double> a) { zero / a; };
Expand Down Expand Up @@ -782,14 +781,6 @@ static_assert(2 * one / (1 * m) == 2 / (1 * m));
// zero
static_assert(quantity<si::metre>{zero} == 0 * m);

static_assert((1 * m += zero) == 1 * m);
static_assert((1 * m -= zero) == 1 * m);

static_assert(1 * m + zero == 1 * m);
static_assert(zero + 1 * m == 1 * m);
static_assert(1 * m - zero == 1 * m);
static_assert(zero - 1 * m == -1 * m);

static_assert(0 * m == zero);
static_assert(1 * m != zero);
static_assert(-1 * m != zero);
Expand Down

0 comments on commit 0dd28c1

Please sign in to comment.