mirror of
https://github.com/mpusz/mp-units.git
synced 2025-07-29 18:07:16 +02:00
quantity
constraints refactored
This commit is contained in:
@ -413,8 +413,26 @@ template<Unit U, Scalar Rep = double>
|
||||
class quantity;
|
||||
```
|
||||
|
||||
`units::Quantity` is a concept that is satisfied by a type that is an instantiation of `units::quantity`
|
||||
class template:
|
||||
where `Scalar` is the following concept:
|
||||
|
||||
```cpp
|
||||
template<typename T, typename U = T>
|
||||
concept basic-arithmetic = // exposition only
|
||||
std::magma<std::ranges::plus, T, U> &&
|
||||
std::magma<std::ranges::minus, T, U> &&
|
||||
std::magma<std::ranges::times, T, U> &&
|
||||
std::magma<std::ranges::divided_by, T, U>;
|
||||
|
||||
template<typename T>
|
||||
concept Scalar =
|
||||
(!Quantity<T>) &&
|
||||
std::regular<T> &&
|
||||
std::totally_ordered<T> &&
|
||||
basic-arithmetic<T>;
|
||||
```
|
||||
|
||||
`units::Quantity` is a concept that is satisfied by a type that is an instantiation of
|
||||
`units::quantity` class template:
|
||||
|
||||
```cpp
|
||||
template<typename T>
|
||||
|
@ -23,51 +23,31 @@
|
||||
#pragma once
|
||||
|
||||
#include <units/bits/hacks.h>
|
||||
#include <units/bits/numeric_concepts.h>
|
||||
#include <units/bits/customization_points.h>
|
||||
#include <units/ratio.h>
|
||||
|
||||
namespace units {
|
||||
|
||||
#if __GNUC__ < 10
|
||||
namespace detail {
|
||||
|
||||
template<typename T>
|
||||
concept Number = std::regular<T> &&
|
||||
std::totally_ordered<T> &&
|
||||
requires(T a, T b) {
|
||||
{ a + b } -> T;
|
||||
{ a - b } -> T;
|
||||
{ a * b } -> T;
|
||||
{ a / b } -> T;
|
||||
{ +a } -> T;
|
||||
{ -a } -> T;
|
||||
{ a += b } -> T&;
|
||||
{ a -= b } -> T&;
|
||||
{ a *= b } -> T&;
|
||||
{ a /= b } -> T&;
|
||||
{ T{0} }; // can construct a T from a zero
|
||||
// …
|
||||
};
|
||||
template<typename T, typename U = T>
|
||||
concept basic_arithmetic = // exposition only
|
||||
std::magma<std::ranges::plus, T, U> &&
|
||||
std::magma<std::ranges::minus, T, U> &&
|
||||
std::magma<std::ranges::times, T, U> &&
|
||||
std::magma<std::ranges::divided_by, T, U>;
|
||||
|
||||
#else
|
||||
template<typename From, typename To>
|
||||
concept safe_convertible = // exposition only
|
||||
std::convertible_to<From, To> &&
|
||||
(treat_as_floating_point<To> || (!treat_as_floating_point<From>));
|
||||
|
||||
template<typename T>
|
||||
concept Number = std::regular<T> &&
|
||||
std::totally_ordered<T> &&
|
||||
requires(T a, T b) {
|
||||
{ a + b } -> std::same_as<T>;
|
||||
{ a - b } -> std::same_as<T>;
|
||||
{ a * b } -> std::same_as<T>;
|
||||
{ a / b } -> std::same_as<T>;
|
||||
{ +a } -> std::same_as<T>;
|
||||
{ -a } -> std::same_as<T>;
|
||||
{ a += b } -> std::same_as<T&>;
|
||||
{ a -= b } -> std::same_as<T&>;
|
||||
{ a *= b } -> std::same_as<T&>;
|
||||
{ a /= b } -> std::same_as<T&>;
|
||||
{ T{0} }; // can construct a T from a zero
|
||||
// …
|
||||
};
|
||||
template<typename Rep, typename unit_from, typename unit_to>
|
||||
concept safe_divisible = // exposition only
|
||||
treat_as_floating_point<Rep> ||
|
||||
ratio_divide<typename unit_from::ratio, typename unit_to::ratio>::den == 1;
|
||||
|
||||
#endif
|
||||
|
||||
// InstanceOf
|
||||
}
|
||||
|
||||
} // namespace units
|
||||
|
@ -23,6 +23,7 @@
|
||||
#pragma once
|
||||
|
||||
#include <concepts/concepts.hpp>
|
||||
#include <functional>
|
||||
|
||||
#ifdef NDEBUG
|
||||
#define Expects(cond) (void)(cond);
|
||||
@ -33,17 +34,34 @@
|
||||
|
||||
#if __GNUC__ > 9
|
||||
#define AUTO auto
|
||||
#define SAME_AS(T) std::same_as<T>
|
||||
#else
|
||||
#define AUTO
|
||||
#define SAME_AS(T) T
|
||||
#endif
|
||||
|
||||
namespace std {
|
||||
|
||||
// concepts
|
||||
using concepts::same_as;
|
||||
using concepts::derived_from;
|
||||
using concepts::regular;
|
||||
using concepts::totally_ordered;
|
||||
using concepts::common_with;
|
||||
using concepts::common_reference_with;
|
||||
using concepts::constructible_from;
|
||||
using concepts::convertible_to;
|
||||
using concepts::default_constructible;
|
||||
using concepts::derived_from;
|
||||
using concepts::equality_comparable_with;
|
||||
using concepts::regular;
|
||||
using concepts::same_as;
|
||||
using concepts::totally_ordered;
|
||||
using concepts::totally_ordered_with;
|
||||
|
||||
template<class F, class... Args>
|
||||
concept invocable =
|
||||
requires(F&& f, Args&&... args) {
|
||||
std::invoke(std::forward<F>(f), std::forward<Args>(args)...);
|
||||
};
|
||||
|
||||
template<class F, class... Args>
|
||||
concept regular_invocable = invocable<F, Args...>;
|
||||
|
||||
}
|
||||
|
@ -117,14 +117,14 @@ namespace std {
|
||||
|
||||
template<class BOp, class T, class U>
|
||||
concept magma =
|
||||
std::common_with<T, U> &&
|
||||
std::regular_invocable<BOp, T, T> &&
|
||||
std::regular_invocable<BOp, U, U> &&
|
||||
std::regular_invocable<BOp, T, U> &&
|
||||
std::regular_invocable<BOp, U, T> &&
|
||||
std::common_with<std::invoke_result_t<BOp&, T, U>, T> &&
|
||||
std::common_with<std::invoke_result_t<BOp&, T, U>, U> &&
|
||||
std::same_as<std::invoke_result_t<BOp&, T, U>, std::invoke_result_t<BOp&, U, T>>;
|
||||
common_with<T, U> &&
|
||||
regular_invocable<BOp, T, T> &&
|
||||
regular_invocable<BOp, U, U> &&
|
||||
regular_invocable<BOp, T, U> &&
|
||||
regular_invocable<BOp, U, T> &&
|
||||
common_with<invoke_result_t<BOp&, T, U>, T> &&
|
||||
common_with<invoke_result_t<BOp&, T, U>, U> &&
|
||||
same_as<invoke_result_t<BOp&, T, U>, invoke_result_t<BOp&, U, T>>;
|
||||
|
||||
template<class BOp, class T, class U>
|
||||
concept semigroup = magma<BOp, T, U>;
|
||||
@ -166,7 +166,6 @@ namespace std {
|
||||
};
|
||||
|
||||
|
||||
|
||||
namespace ranges {
|
||||
|
||||
namespace detail {
|
||||
@ -200,7 +199,7 @@ namespace std {
|
||||
|
||||
template<class T>
|
||||
concept negatable = // exposition only
|
||||
summable_with <T, T> &&
|
||||
summable_with<T, T> &&
|
||||
totally_ordered<T> &&
|
||||
requires(T&& t) {
|
||||
{ -std::forward<T>(t) } -> common_with<T>;
|
||||
@ -231,7 +230,7 @@ namespace std {
|
||||
{ std::forward<T>(t) - std::forward<U>(u) } -> common_with<T>;
|
||||
{ std::forward<U>(u) - std::forward<T>(t) } -> common_with<U>;
|
||||
requires same_as<decltype(std::forward<T>(t) - std::forward<U>(u)),
|
||||
decltype(std::forward<U>(u) - std::forward<T>(t))>;
|
||||
decltype(std::forward<U>(u) - std::forward<T>(t))>;
|
||||
};
|
||||
|
||||
}
|
||||
@ -249,10 +248,10 @@ namespace std {
|
||||
|
||||
template<class T, class U>
|
||||
concept multiplicable_with = // exposition only
|
||||
detail::summable_with <T, U> &&
|
||||
detail::summable_with<T, U> &&
|
||||
constructible_from<remove_cvref_t<T>, int> && // specifically T{0} and T{1}
|
||||
constructible_from<remove_cvref_t<U>, int> && // specifically U{0} and U{1}
|
||||
constructible_from<remove_cvref_t<common_type<T, U>>, int> &&
|
||||
// constructible_from<remove_cvref_t<common_type<T, U>>, int> && // TODO uncomment this when the problem is resolved
|
||||
common_reference_with<T, U> &&
|
||||
requires(T&& t, U&& u) {
|
||||
{ std::forward<T>(t) * std::forward<T>(t) } -> common_with<T>;
|
||||
@ -260,7 +259,7 @@ namespace std {
|
||||
{ std::forward<T>(t) * std::forward<U>(u) } -> common_with<T>;
|
||||
{ std::forward<U>(u) * std::forward<T>(t) } -> common_with<U>;
|
||||
requires same_as<decltype(std::forward<T>(t) * std::forward<U>(u)),
|
||||
decltype(std::forward<U>(u) * std::forward<T>(t))>;
|
||||
decltype(std::forward<U>(u) * std::forward<T>(t))>;
|
||||
};
|
||||
|
||||
}
|
||||
@ -285,7 +284,7 @@ namespace std {
|
||||
{ std::forward<T>(t) / std::forward<U>(u) } -> common_with<T>;
|
||||
{ std::forward<U>(u) / std::forward<T>(t) } -> common_with<U>;
|
||||
requires same_as<decltype(std::forward<T>(t) / std::forward<U>(u)),
|
||||
decltype(std::forward<U>(u) / std::forward<T>(t))>;
|
||||
decltype(std::forward<U>(u) / std::forward<T>(t))>;
|
||||
};
|
||||
|
||||
}
|
||||
@ -300,14 +299,14 @@ namespace std {
|
||||
|
||||
template<class T, class Q>
|
||||
concept modulo_with = // exposition only
|
||||
divisible_with <T, Q> &&
|
||||
divisible_with<T, Q> &&
|
||||
requires(T&& t, Q&& q) {
|
||||
{ std::forward<T>(t) % std::forward<T>(t) } -> common_with<T>;
|
||||
{ std::forward<Q>(q) % std::forward<Q>(q) } -> common_with<Q>;
|
||||
{ std::forward<T>(t) % std::forward<Q>(q) } -> common_with<T>;
|
||||
{ std::forward<Q>(q) % std::forward<T>(t) } -> common_with<Q>;
|
||||
requires same_as<decltype(std::forward<T>(t) % std::forward<Q>(q)),
|
||||
decltype(std::forward<Q>(q) % std::forward<T>(t))>;
|
||||
decltype(std::forward<Q>(q) % std::forward<T>(t))>;
|
||||
};
|
||||
|
||||
}
|
||||
|
@ -23,7 +23,6 @@
|
||||
#pragma once
|
||||
|
||||
#include <units/bits/concepts.h>
|
||||
#include <units/bits/customization_points.h>
|
||||
#include <units/unit.h>
|
||||
#include <limits>
|
||||
#include <ostream>
|
||||
@ -48,27 +47,13 @@ namespace units {
|
||||
|
||||
// Scalar
|
||||
template<typename T>
|
||||
concept Scalar = (!Quantity<T>) && Number<T>;
|
||||
concept Scalar =
|
||||
(!Quantity<T>) &&
|
||||
std::regular<T> &&
|
||||
std::totally_ordered<T> &&
|
||||
detail::basic_arithmetic<T>;
|
||||
|
||||
// QuantityRep
|
||||
template<typename T>
|
||||
concept QuantityRep = Scalar<T> &&
|
||||
// integral
|
||||
((treat_as_floating_point<T> == false &&
|
||||
requires(T a, T b) {
|
||||
{ a % b } -> T;
|
||||
{ a++ } -> T;
|
||||
{ ++a } -> T&;
|
||||
{ a-- } -> T;
|
||||
{ --a } -> T&;
|
||||
}) ||
|
||||
// floating-point
|
||||
requires(T&& a) {
|
||||
::units::isfinite(std::forward<T>(a));
|
||||
::units::isnan(std::forward<T>(a));
|
||||
});
|
||||
|
||||
template<Unit U, QuantityRep Rep>
|
||||
template<Unit U, Scalar Rep>
|
||||
class quantity;
|
||||
|
||||
namespace detail {
|
||||
@ -99,7 +84,7 @@ namespace units {
|
||||
|
||||
} // namespace detail
|
||||
|
||||
template<Quantity Q1, Quantity Q2, QuantityRep Rep = std::common_type_t<typename Q1::rep, typename Q2::rep>>
|
||||
template<Quantity Q1, Quantity Q2, Scalar Rep = std::common_type_t<typename Q1::rep, typename Q2::rep>>
|
||||
using common_quantity = detail::common_quantity_impl<Q1, Q2, Rep>::type;
|
||||
|
||||
// quantity_cast
|
||||
@ -158,7 +143,8 @@ namespace units {
|
||||
|
||||
template<Quantity To, typename U, typename Rep>
|
||||
[[nodiscard]] constexpr auto quantity_cast(const quantity<U, Rep>& q)
|
||||
requires same_dim<typename To::dimension, typename U::dimension>
|
||||
requires same_dim<typename To::dimension, typename U::dimension> &&
|
||||
detail::basic_arithmetic<std::common_type_t<typename To::rep, Rep, intmax_t>>
|
||||
{
|
||||
using c_ratio = ratio_divide<typename U::ratio, typename To::unit::ratio>;
|
||||
using c_rep = std::common_type_t<typename To::rep, Rep, intmax_t>;
|
||||
@ -169,27 +155,31 @@ namespace units {
|
||||
return cast::cast(q);
|
||||
}
|
||||
|
||||
template<Unit ToU, QuantityRep ToRep, typename U, typename Rep>
|
||||
template<Unit ToU, Scalar ToRep, typename U, typename Rep>
|
||||
[[nodiscard]] constexpr auto quantity_cast(const quantity<U, Rep>& q)
|
||||
requires same_dim<typename ToU::dimension, typename U::dimension> &&
|
||||
detail::basic_arithmetic<std::common_type_t<ToRep, Rep, intmax_t>>
|
||||
{
|
||||
return quantity_cast<quantity<ToU, ToRep>>(q);
|
||||
}
|
||||
|
||||
template<Unit ToU, typename U, typename Rep>
|
||||
[[nodiscard]] constexpr auto quantity_cast(const quantity<U, Rep>& q)
|
||||
requires same_dim<typename ToU::dimension, typename U::dimension>
|
||||
{
|
||||
return quantity_cast<quantity<ToU, Rep>>(q);
|
||||
}
|
||||
|
||||
template<QuantityRep ToRep, typename U, typename Rep>
|
||||
template<Scalar ToRep, typename U, typename Rep>
|
||||
[[nodiscard]] constexpr auto quantity_cast(const quantity<U, Rep>& q)
|
||||
requires detail::basic_arithmetic<std::common_type_t<ToRep, Rep, intmax_t>>
|
||||
{
|
||||
return quantity_cast<quantity<U, ToRep>>(q);
|
||||
}
|
||||
|
||||
// quantity_values
|
||||
|
||||
template<QuantityRep Rep>
|
||||
template<Scalar Rep>
|
||||
struct quantity_values {
|
||||
static constexpr Rep zero() noexcept { return Rep(0); }
|
||||
static constexpr Rep one() noexcept { return Rep(1); }
|
||||
@ -197,9 +187,10 @@ namespace units {
|
||||
static constexpr Rep min() noexcept { return std::numeric_limits<Rep>::lowest(); }
|
||||
};
|
||||
|
||||
|
||||
// quantity
|
||||
|
||||
template<Unit U, QuantityRep Rep = double>
|
||||
template<Unit U, Scalar Rep = double>
|
||||
class quantity {
|
||||
Rep value_;
|
||||
|
||||
@ -213,18 +204,15 @@ namespace units {
|
||||
quantity(quantity&&) = default;
|
||||
|
||||
template<Scalar Value>
|
||||
requires std::convertible_to<Value, rep> &&
|
||||
(treat_as_floating_point<rep> || (!treat_as_floating_point<Value>))
|
||||
requires detail::safe_convertible<Value, rep>
|
||||
constexpr explicit quantity(const Value& r): value_{static_cast<rep>(r)}
|
||||
{
|
||||
}
|
||||
|
||||
template<Quantity Q2>
|
||||
requires same_dim<dimension, typename Q2::dimension> &&
|
||||
std::convertible_to<typename Q2::rep, rep> &&
|
||||
(treat_as_floating_point<rep> ||
|
||||
(std::ratio_divide<typename Q2::unit::ratio, typename unit::ratio>::den == 1 &&
|
||||
!treat_as_floating_point<typename Q2::rep>))
|
||||
requires same_dim<dimension, typename Q2::dimension> &&
|
||||
detail::safe_convertible<typename Q2::rep, rep> &&
|
||||
detail::safe_divisible<rep, typename Q2::unit, unit>
|
||||
constexpr quantity(const Q2& q): value_{quantity_cast<quantity>(q).count()}
|
||||
{
|
||||
}
|
||||
@ -234,68 +222,130 @@ namespace units {
|
||||
|
||||
[[nodiscard]] constexpr rep count() const noexcept { return value_; }
|
||||
|
||||
[[nodiscard]] static constexpr quantity zero() noexcept { return quantity(quantity_values<Rep>::zero()); }
|
||||
[[nodiscard]] static constexpr quantity one() noexcept { return quantity(quantity_values<Rep>::one()); }
|
||||
[[nodiscard]] static constexpr quantity min() noexcept { return quantity(quantity_values<Rep>::min()); }
|
||||
[[nodiscard]] static constexpr quantity max() noexcept { return quantity(quantity_values<Rep>::max()); }
|
||||
template<typename T = Rep>
|
||||
[[nodiscard]] static constexpr quantity zero() noexcept
|
||||
requires requires { quantity_values<T>::zero(); }
|
||||
// requires requires { quantity_values<Rep>::zero(); } // TODO gated by gcc-9 (fixed in gcc-10)
|
||||
{
|
||||
return quantity(quantity_values<Rep>::zero());
|
||||
}
|
||||
|
||||
template<typename T = Rep>
|
||||
[[nodiscard]] static constexpr quantity one() noexcept
|
||||
requires requires { quantity_values<T>::one(); }
|
||||
// requires requires { quantity_values<Rep>::one(); } // TODO gated by gcc-9 (fixed in gcc-10)
|
||||
{
|
||||
return quantity(quantity_values<Rep>::one());
|
||||
}
|
||||
|
||||
template<typename T = Rep>
|
||||
[[nodiscard]] static constexpr quantity min() noexcept
|
||||
requires requires { quantity_values<T>::min(); }
|
||||
// requires requires { quantity_values<Rep>::min(); } // TODO gated by gcc-9 (fixed in gcc-10)
|
||||
{
|
||||
return quantity(quantity_values<Rep>::min());
|
||||
}
|
||||
|
||||
template<typename T = Rep>
|
||||
[[nodiscard]] static constexpr quantity max() noexcept
|
||||
requires requires { quantity_values<T>::max(); }
|
||||
// requires requires { quantity_values<Rep>::max(); } // TODO gated by gcc-9 (fixed in gcc-10)
|
||||
{
|
||||
return quantity(quantity_values<Rep>::max());
|
||||
}
|
||||
|
||||
[[nodiscard]] constexpr quantity operator+() const { return *this; }
|
||||
[[nodiscard]] constexpr quantity operator-() const { return quantity(-count()); }
|
||||
|
||||
template<typename T = Rep>
|
||||
[[nodiscard]] constexpr quantity operator-() const
|
||||
// requires std::magma<std::ranges::negate, T, T>
|
||||
// requires std::magma<std::ranges::negate, rep, rep> // TODO gated by gcc-9 (fixed in gcc-10)
|
||||
{
|
||||
return quantity(-count());
|
||||
}
|
||||
|
||||
template<typename T = Rep>
|
||||
constexpr quantity& operator++()
|
||||
// requires requires(rep v) { { ++v } -> std::same_as<rep&>; } // TODO gated by gcc-9 (fixed in gcc-10)
|
||||
requires requires(T v) { { ++v } -> SAME_AS(T&); }
|
||||
// requires requires(rep v) { { ++v } -> std::same_as<rep&>; } // TODO gated by gcc-9 (fixed in gcc-10)
|
||||
{
|
||||
++value_;
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<typename T = Rep>
|
||||
requires requires(T v) { { v++ } -> SAME_AS(T); }
|
||||
constexpr quantity operator++(int)
|
||||
// requires requires(rep v) { { v++ } -> std::same_as<rep>; } // TODO gated by gcc-9 (fixed in gcc-10)
|
||||
{ return quantity(value_++); }
|
||||
|
||||
template<typename T = Rep>
|
||||
requires requires(T v) { { --v } -> SAME_AS(T&); }
|
||||
constexpr quantity& operator--()
|
||||
// requires requires(rep v) { { --v } -> std::same_as<rep&>; } // TODO gated by gcc-9 (fixed in gcc-10)
|
||||
{
|
||||
--value_;
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<typename T = Rep>
|
||||
requires requires(T v) { { v-- } -> SAME_AS(T); }
|
||||
constexpr quantity operator--(int)
|
||||
// requires requires(rep v) { { v-- } -> std::same_as<rep>; } // TODO gated by gcc-9 (fixed in gcc-10)
|
||||
{ return quantity(value_--); }
|
||||
|
||||
template<typename T = Rep>
|
||||
requires requires(T v1, T v2) { { v1 += v2 } -> SAME_AS(T&); }
|
||||
constexpr quantity& operator+=(const quantity& q)
|
||||
// requires requires(rep v1, rep v2) { { v1 += v2 } -> std::same_as<T&>; } // TODO gated by gcc-9 (fixed in gcc-10)
|
||||
{
|
||||
value_ += q.count();
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<typename T = Rep>
|
||||
requires requires(T v1, T v2) { { v1 -= v2 } -> SAME_AS(T&); }
|
||||
constexpr quantity& operator-=(const quantity& q)
|
||||
// requires requires(rep v1, rep v2) { { v1 -= v2 } -> std::same_as<T&>; } // TODO gated by gcc-9 (fixed in gcc-10)
|
||||
{
|
||||
value_ -= q.count();
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<typename T = Rep>
|
||||
requires requires(T v1, T v2) { { v1 *= v2 } -> SAME_AS(T&); }
|
||||
constexpr quantity& operator*=(const rep& rhs)
|
||||
// requires requires(rep v1, rep v2) { { v1 *= v2 } -> std::same_as<T&>; } // TODO gated by gcc-9 (fixed in gcc-10)
|
||||
{
|
||||
value_ *= rhs;
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<typename T = Rep>
|
||||
requires requires(T v1, T v2) { { v1 /= v2 } -> SAME_AS(T&); }
|
||||
constexpr quantity& operator/=(const rep& rhs)
|
||||
// requires requires(rep v1, rep v2) { { v1 /= v2 } -> std::same_as<rep&>; } // TODO gated by gcc-9 (fixed in gcc-10)
|
||||
{
|
||||
value_ /= rhs;
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<Scalar Value>
|
||||
template<Scalar Value, typename T = Rep>
|
||||
constexpr quantity& operator%=(const Value& rhs)
|
||||
requires (!treat_as_floating_point<rep> && !treat_as_floating_point<Value>)
|
||||
requires (!treat_as_floating_point<rep>) &&
|
||||
(!treat_as_floating_point<Value>) &&
|
||||
requires(T v1, Value v2) { { v1 %= v2 } -> SAME_AS(T&); }
|
||||
// requires(rep v1, Value v2) { { v1 %= v2 } -> SAME_AS(rep&); } // TODO gated by gcc-9 (fixed in gcc-10)
|
||||
{
|
||||
value_ %= rhs;
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<typename T = Rep>
|
||||
constexpr quantity& operator%=(const quantity& q)
|
||||
requires (!treat_as_floating_point<rep>)
|
||||
requires (!treat_as_floating_point<rep>) &&
|
||||
requires(T v1, T v2) { { v1 %= v2 } -> SAME_AS(T&); }
|
||||
// requires(rep v1, rep v2) { { v1 %= v2 } -> std::same_as<rep&>; } // TODO gated by gcc-9 (fixed in gcc-10)
|
||||
{
|
||||
value_ %= q.count();
|
||||
return *this;
|
||||
@ -310,7 +360,7 @@ namespace units {
|
||||
|
||||
template<typename U1, typename Rep1, typename U2, typename Rep2>
|
||||
[[nodiscard]] constexpr Quantity AUTO operator+(const quantity<U1, Rep1>& lhs, const quantity<U2, Rep2>& rhs)
|
||||
requires same_dim<typename U1::dimension, typename U2::dimension>
|
||||
requires same_dim<typename U1::dimension, typename U2::dimension> && detail::basic_arithmetic<Rep1, Rep2>
|
||||
{
|
||||
using common_rep = decltype(lhs.count() + rhs.count());
|
||||
using ret = common_quantity<quantity<U1, Rep1>, quantity<U2, Rep2>, common_rep>;
|
||||
@ -319,30 +369,32 @@ namespace units {
|
||||
|
||||
template<typename U1, typename Rep1, typename U2, typename Rep2>
|
||||
[[nodiscard]] constexpr Quantity AUTO operator-(const quantity<U1, Rep1>& lhs, const quantity<U2, Rep2>& rhs)
|
||||
requires same_dim<typename U1::dimension, typename U2::dimension>
|
||||
requires same_dim<typename U1::dimension, typename U2::dimension> && detail::basic_arithmetic<Rep1, Rep2>
|
||||
{
|
||||
using common_rep = decltype(lhs.count() - rhs.count());
|
||||
using ret = common_quantity<quantity<U1, Rep1>, quantity<U2, Rep2>, common_rep>;
|
||||
return ret(ret(lhs).count() - ret(rhs).count());
|
||||
}
|
||||
|
||||
template<typename U, typename Rep1, Scalar Value>
|
||||
[[nodiscard]] constexpr Quantity AUTO operator*(const quantity<U, Rep1>& q, const Value& v)
|
||||
template<typename U, typename Rep, Scalar Value>
|
||||
[[nodiscard]] constexpr Quantity AUTO operator*(const quantity<U, Rep>& q, const Value& v)
|
||||
requires detail::basic_arithmetic<Rep, Value>
|
||||
{
|
||||
using common_rep = decltype(q.count() * v);
|
||||
using ret = quantity<U, common_rep>;
|
||||
return ret(q.count() * v);
|
||||
}
|
||||
|
||||
template<Scalar Value, typename U, typename Rep2>
|
||||
[[nodiscard]] constexpr Quantity AUTO operator*(const Value& v, const quantity<U, Rep2>& q)
|
||||
template<Scalar Value, typename U, typename Rep>
|
||||
[[nodiscard]] constexpr Quantity AUTO operator*(const Value& v, const quantity<U, Rep>& q)
|
||||
requires detail::basic_arithmetic<Value, Rep>
|
||||
{
|
||||
return q * v;
|
||||
}
|
||||
|
||||
template<typename U1, typename Rep1, typename U2, typename Rep2>
|
||||
[[nodiscard]] constexpr QuantityRep AUTO operator*(const quantity<U1, Rep1>& lhs, const quantity<U2, Rep2>& rhs)
|
||||
requires same_dim<typename U1::dimension, dim_invert<typename U2::dimension>>
|
||||
[[nodiscard]] constexpr Scalar AUTO operator*(const quantity<U1, Rep1>& lhs, const quantity<U2, Rep2>& rhs)
|
||||
requires same_dim<typename U1::dimension, dim_invert<typename U2::dimension>> && detail::basic_arithmetic<Rep1, Rep2>
|
||||
{
|
||||
using common_rep = decltype(lhs.count() * rhs.count());
|
||||
using ratio = ratio_multiply<typename U1::ratio, typename U2::ratio>;
|
||||
@ -353,7 +405,8 @@ namespace units {
|
||||
[[nodiscard]] constexpr Quantity AUTO operator*(const quantity<U1, Rep1>& lhs, const quantity<U2, Rep2>& rhs)
|
||||
requires (!same_dim<typename U1::dimension, dim_invert<typename U2::dimension>>) &&
|
||||
(treat_as_floating_point<decltype(lhs.count() * rhs.count())> ||
|
||||
(std::ratio_multiply<typename U1::ratio, typename U2::ratio>::den == 1))
|
||||
(std::ratio_multiply<typename U1::ratio, typename U2::ratio>::den == 1)) &&
|
||||
detail::basic_arithmetic<Rep1, Rep2>
|
||||
{
|
||||
using dim = dimension_multiply<typename U1::dimension, typename U2::dimension>;
|
||||
using common_rep = decltype(lhs.count() * rhs.count());
|
||||
@ -363,6 +416,7 @@ namespace units {
|
||||
|
||||
template<Scalar Value, typename U, typename Rep>
|
||||
[[nodiscard]] constexpr Quantity AUTO operator/(const Value& v, const quantity<U, Rep>& q)
|
||||
requires detail::basic_arithmetic<Value, Rep>
|
||||
{
|
||||
Expects(q != std::remove_cvref_t<decltype(q)>(0));
|
||||
|
||||
@ -374,6 +428,7 @@ namespace units {
|
||||
|
||||
template<typename U, typename Rep, Scalar Value>
|
||||
[[nodiscard]] constexpr Quantity AUTO operator/(const quantity<U, Rep>& q, const Value& v)
|
||||
requires detail::basic_arithmetic<Rep, Value>
|
||||
{
|
||||
Expects(v != Value{0});
|
||||
|
||||
@ -383,8 +438,8 @@ namespace units {
|
||||
}
|
||||
|
||||
template<typename U1, typename Rep1, typename U2, typename Rep2>
|
||||
[[nodiscard]] constexpr QuantityRep AUTO operator/(const quantity<U1, Rep1>& lhs, const quantity<U2, Rep2>& rhs)
|
||||
requires same_dim<typename U1::dimension, typename U2::dimension>
|
||||
[[nodiscard]] constexpr Scalar AUTO operator/(const quantity<U1, Rep1>& lhs, const quantity<U2, Rep2>& rhs)
|
||||
requires same_dim<typename U1::dimension, typename U2::dimension> && detail::basic_arithmetic<Rep1, Rep2>
|
||||
{
|
||||
Expects(rhs != std::remove_cvref_t<decltype(rhs)>(0));
|
||||
|
||||
@ -397,7 +452,8 @@ namespace units {
|
||||
[[nodiscard]] constexpr Quantity AUTO operator/(const quantity<U1, Rep1>& lhs, const quantity<U2, Rep2>& rhs)
|
||||
requires (!same_dim<typename U1::dimension, typename U2::dimension>) &&
|
||||
(treat_as_floating_point<decltype(lhs.count() / rhs.count())> ||
|
||||
(ratio_divide<typename U1::ratio, typename U2::ratio>::den == 1))
|
||||
(ratio_divide<typename U1::ratio, typename U2::ratio>::den == 1)) &&
|
||||
detail::basic_arithmetic<Rep1, Rep2>
|
||||
{
|
||||
Expects(rhs != std::remove_cvref_t<decltype(rhs)>(0));
|
||||
|
||||
@ -409,7 +465,8 @@ namespace units {
|
||||
|
||||
template<typename U, typename Rep, Scalar Value>
|
||||
[[nodiscard]] constexpr Quantity AUTO operator%(const quantity<U, Rep>& q, const Value& v)
|
||||
requires (!treat_as_floating_point<Rep> && !treat_as_floating_point<Value>)
|
||||
requires (!treat_as_floating_point<Rep>) && (!treat_as_floating_point<Value>) &&
|
||||
detail::basic_arithmetic<Rep, Value> && std::magma<std::ranges::modulus, Rep, Value>
|
||||
{
|
||||
using common_rep = decltype(q.count() % v);
|
||||
using ret = quantity<U, common_rep>;
|
||||
@ -418,7 +475,8 @@ namespace units {
|
||||
|
||||
template<typename U1, typename Rep1, typename U2, typename Rep2>
|
||||
[[nodiscard]] constexpr Quantity AUTO operator%(const quantity<U1, Rep1>& lhs, const quantity<U2, Rep2>& rhs)
|
||||
requires (!treat_as_floating_point<Rep1> && !treat_as_floating_point<Rep2>)
|
||||
requires (!treat_as_floating_point<Rep1>) && (!treat_as_floating_point<Rep2>) &&
|
||||
detail::basic_arithmetic<Rep1, Rep2> && std::magma<std::ranges::modulus, Rep1, Rep2>
|
||||
{
|
||||
using common_rep = decltype(lhs.count() % rhs.count());
|
||||
using ret = common_quantity<quantity<U1, Rep1>, quantity<U2, Rep2>, common_rep>;
|
||||
@ -427,7 +485,8 @@ namespace units {
|
||||
|
||||
template<typename U1, typename Rep1, typename U2, typename Rep2>
|
||||
[[nodiscard]] constexpr bool operator==(const quantity<U1, Rep1>& lhs, const quantity<U2, Rep2>& rhs)
|
||||
requires same_dim<typename U1::dimension, typename U2::dimension>
|
||||
requires same_dim<typename U1::dimension, typename U2::dimension> &&
|
||||
detail::basic_arithmetic<Rep1, Rep2> && std::equality_comparable_with<Rep1, Rep2>
|
||||
{
|
||||
using cq = common_quantity<quantity<U1, Rep1>, quantity<U2, Rep2>>;
|
||||
return cq(lhs).count() == cq(rhs).count();
|
||||
@ -435,14 +494,16 @@ namespace units {
|
||||
|
||||
template<typename U1, typename Rep1, typename U2, typename Rep2>
|
||||
[[nodiscard]] constexpr bool operator!=(const quantity<U1, Rep1>& lhs, const quantity<U2, Rep2>& rhs)
|
||||
requires same_dim<typename U1::dimension, typename U2::dimension>
|
||||
requires same_dim<typename U1::dimension, typename U2::dimension> &&
|
||||
detail::basic_arithmetic<Rep1, Rep2> && std::equality_comparable_with<Rep1, Rep2>
|
||||
{
|
||||
return !(lhs == rhs);
|
||||
}
|
||||
|
||||
template<typename U1, typename Rep1, typename U2, typename Rep2>
|
||||
[[nodiscard]] constexpr bool operator<(const quantity<U1, Rep1>& lhs, const quantity<U2, Rep2>& rhs)
|
||||
requires same_dim<typename U1::dimension, typename U2::dimension>
|
||||
requires same_dim<typename U1::dimension, typename U2::dimension> &&
|
||||
detail::basic_arithmetic<Rep1, Rep2> && std::totally_ordered_with<Rep1, Rep2>
|
||||
{
|
||||
using cq = common_quantity<quantity<U1, Rep1>, quantity<U2, Rep2>>;
|
||||
return cq(lhs).count() < cq(rhs).count();
|
||||
@ -450,21 +511,24 @@ namespace units {
|
||||
|
||||
template<typename U1, typename Rep1, typename U2, typename Rep2>
|
||||
[[nodiscard]] constexpr bool operator<=(const quantity<U1, Rep1>& lhs, const quantity<U2, Rep2>& rhs)
|
||||
requires same_dim<typename U1::dimension, typename U2::dimension>
|
||||
requires same_dim<typename U1::dimension, typename U2::dimension> &&
|
||||
detail::basic_arithmetic<Rep1, Rep2> && std::totally_ordered_with<Rep1, Rep2>
|
||||
{
|
||||
return !(rhs < lhs);
|
||||
}
|
||||
|
||||
template<typename U1, typename Rep1, typename U2, typename Rep2>
|
||||
[[nodiscard]] constexpr bool operator>(const quantity<U1, Rep1>& lhs, const quantity<U2, Rep2>& rhs)
|
||||
requires same_dim<typename U1::dimension, typename U2::dimension>
|
||||
requires same_dim<typename U1::dimension, typename U2::dimension> &&
|
||||
detail::basic_arithmetic<Rep1, Rep2> && std::totally_ordered_with<Rep1, Rep2>
|
||||
{
|
||||
return rhs < lhs;
|
||||
}
|
||||
|
||||
template<typename U1, typename Rep1, typename U2, typename Rep2>
|
||||
[[nodiscard]] constexpr bool operator>=(const quantity<U1, Rep1>& lhs, const quantity<U2, Rep2>& rhs)
|
||||
requires same_dim<typename U1::dimension, typename U2::dimension>
|
||||
requires same_dim<typename U1::dimension, typename U2::dimension> &&
|
||||
detail::basic_arithmetic<Rep1, Rep2> && std::totally_ordered_with<Rep1, Rep2>
|
||||
{
|
||||
return !(lhs < rhs);
|
||||
}
|
||||
|
@ -37,13 +37,27 @@ namespace {
|
||||
public:
|
||||
my_value() = default;
|
||||
constexpr my_value(T v) : value_(std::move(v)) {}
|
||||
constexpr my_value& operator+=(const my_value& other) { value_ += other.value_; return *this; }
|
||||
constexpr my_value& operator-=(const my_value& other) { value_ -= other.value_; return *this; }
|
||||
constexpr my_value& operator*=(const my_value& other) { value_ *= other.value_; return *this; }
|
||||
constexpr my_value& operator/=(const my_value& other) { value_ /= other.value_; return *this; }
|
||||
[[nodiscard]] constexpr my_value operator-() const { return my_value(-value_); }
|
||||
|
||||
[[nodiscard]] friend constexpr my_value operator+(my_value lhs, my_value rhs) { return my_value(lhs.value_ + rhs.value_); }
|
||||
[[nodiscard]] friend constexpr my_value operator-(my_value lhs, my_value rhs) { return my_value(lhs.value_ - rhs.value_); }
|
||||
[[nodiscard]] friend constexpr my_value operator*(my_value lhs, my_value rhs) { return my_value(lhs.value_ * rhs.value_); }
|
||||
[[nodiscard]] friend constexpr my_value operator/(my_value lhs, my_value rhs) { return my_value(lhs.value_ / rhs.value_); }
|
||||
|
||||
[[nodiscard]] friend constexpr bool operator==(my_value lhs, my_value rhs) { return lhs.value_ == rhs.value_; }
|
||||
[[nodiscard]] friend constexpr bool operator!=(my_value lhs, my_value rhs) { return !(lhs == rhs); }
|
||||
[[nodiscard]] friend constexpr bool operator<(my_value lhs, my_value rhs) { return lhs.value_ < rhs.value_; }
|
||||
[[nodiscard]] friend constexpr bool operator>(my_value lhs, my_value rhs) { return rhs < lhs; }
|
||||
[[nodiscard]] friend constexpr bool operator<=(my_value lhs, my_value rhs) { return !(rhs < lhs); }
|
||||
[[nodiscard]] friend constexpr bool operator>=(my_value lhs, my_value rhs) { return !(lhs < rhs); }
|
||||
|
||||
constexpr operator const T&() const & { return value_; }
|
||||
};
|
||||
|
||||
static_assert(units::Scalar<my_value<float>>);
|
||||
static_assert(std::convertible_to<my_value<float>, float>);
|
||||
static_assert(std::convertible_to<float, my_value<float>>);
|
||||
|
||||
} // namespace
|
||||
|
||||
namespace units {
|
||||
@ -96,6 +110,9 @@ namespace {
|
||||
|
||||
// constructors
|
||||
|
||||
using my_int = my_value<int>;
|
||||
using my_double = my_value<double>;
|
||||
|
||||
static_assert(quantity<metre, int>().count() == 0);
|
||||
constexpr quantity<metre, int> km{1000};
|
||||
static_assert(km.count() == 1000);
|
||||
@ -103,7 +120,7 @@ namespace {
|
||||
|
||||
static_assert(quantity<metre, int>(1).count() == 1);
|
||||
static_assert(quantity<metre, int>(my_value(1)).count() == 1);
|
||||
static_assert(quantity<metre, my_value<int>>(1).count() == 1);
|
||||
static_assert(quantity<metre, my_int>(1).count() == my_int{1});
|
||||
// static_assert(quantity<metre, int>(1.0).count() == 1); // should not compile
|
||||
// static_assert(quantity<metre, int>(my_value(1.0)).count() == 1); // should not compile
|
||||
// static_assert(quantity<metre, my_value>(1.0).count() == 1); // should not compile
|
||||
@ -112,24 +129,24 @@ namespace {
|
||||
static_assert(quantity<metre, double>(1).count() == 1.0);
|
||||
static_assert(quantity<metre, double>(my_value(1)).count() == 1.0);
|
||||
static_assert(quantity<metre, double>(3.14).count() == 3.14);
|
||||
static_assert(quantity<metre, my_value<double>>(1.0).count() == 1.0);
|
||||
static_assert(quantity<metre, my_value<double>>(1).count() == 1.0);
|
||||
static_assert(quantity<metre, my_value<double>>(3.14).count() == 3.14);
|
||||
static_assert(quantity<metre, my_double>(1.0).count() == my_double{1.0});
|
||||
static_assert(quantity<metre, my_double>(1).count() == my_double{1.0});
|
||||
static_assert(quantity<metre, my_double>(3.14).count() == my_double{3.14});
|
||||
|
||||
static_assert(quantity<metre, int>(km).count() == 1000);
|
||||
// static_assert(quantity<metre, int>(quantity<metre, double>(3.14)).count() == 3); // should not compile
|
||||
static_assert(quantity<metre, int>(quantity_cast<quantity<metre, my_value<int>>>(3.14m)).count() == 3);
|
||||
// static_assert(quantity<metre, int>(quantity<metre, my_value<double>>(1000.0)).count() == 1000); // should not compile
|
||||
static_assert(quantity<metre, int>(quantity_cast<quantity<metre, my_int>>(3.14m)).count() == 3);
|
||||
// static_assert(quantity<metre, int>(quantity<metre, my_double>(1000.0)).count() == 1000); // should not compile
|
||||
// static_assert(quantity<metre, my_value>(1000.0m).count() == 1000); // should not compile
|
||||
static_assert(quantity<metre, double>(1000.0m).count() == 1000.0);
|
||||
static_assert(quantity<metre, double>(quantity<metre, my_value<double>>(1000.0)).count() == 1000.0);
|
||||
static_assert(quantity<metre, my_value<double>>(1000.0m).count() == 1000.0);
|
||||
static_assert(quantity<metre, double>(quantity<metre, my_double>(1000.0)).count() == 1000.0);
|
||||
static_assert(quantity<metre, my_double>(1000.0m).count() == my_double{1000.0});
|
||||
static_assert(quantity<metre, double>(km).count() == 1000.0);
|
||||
static_assert(quantity<metre, my_value<double>>(km).count() == 1000.0);
|
||||
static_assert(quantity<metre, my_double>(km).count() == my_double{1000.0});
|
||||
static_assert(quantity<metre, int>(1km).count() == 1000);
|
||||
// static_assert(quantity<metre, int>(1_s).count() == 1); // should not compile
|
||||
// static_assert(quantity<kilometre, int>(1010m).count() == 1); // should not compile
|
||||
static_assert(quantity<kilometre, int>(quantity_cast<quantity<kilometre, my_value<int>>>(1010m)).count() == 1);
|
||||
static_assert(quantity<kilometre, int>(quantity_cast<quantity<kilometre, my_int>>(1010m)).count() == 1);
|
||||
|
||||
// assignment operator
|
||||
|
||||
@ -146,12 +163,12 @@ namespace {
|
||||
static_assert(quantity<metre, double>::zero().count() == 0.0);
|
||||
static_assert(quantity<metre, double>::min().count() == std::numeric_limits<double>::lowest());
|
||||
static_assert(quantity<metre, double>::max().count() == std::numeric_limits<double>::max());
|
||||
static_assert(quantity<metre, my_value<int>>::zero().count() == 0);
|
||||
static_assert(quantity<metre, my_value<int>>::min().count() == std::numeric_limits<int>::lowest());
|
||||
static_assert(quantity<metre, my_value<int>>::max().count() == std::numeric_limits<int>::max());
|
||||
static_assert(quantity<metre, my_value<double>>::zero().count() == 0.0);
|
||||
static_assert(quantity<metre, my_value<double>>::min().count() == std::numeric_limits<double>::lowest());
|
||||
static_assert(quantity<metre, my_value<double>>::max().count() == std::numeric_limits<double>::max());
|
||||
static_assert(quantity<metre, my_int>::zero().count() == my_int{0});
|
||||
static_assert(quantity<metre, my_int>::min().count() == my_int{std::numeric_limits<int>::lowest()});
|
||||
static_assert(quantity<metre, my_int>::max().count() == my_int{std::numeric_limits<int>::max()});
|
||||
static_assert(quantity<metre, my_double>::zero().count() == my_double{0.0});
|
||||
static_assert(quantity<metre, my_double>::min().count() == my_double{std::numeric_limits<double>::lowest()});
|
||||
static_assert(quantity<metre, my_double>::max().count() == my_double{std::numeric_limits<double>::max()});
|
||||
|
||||
// unary member operators
|
||||
|
||||
|
Reference in New Issue
Block a user