Overconstrained quantity operations relaxed
This commit is contained in:
1 parent
2a5baff160
commit
ab1cc4b8e7
6 files changed
+167
-95
No files matched your search
@@ -22,7 +22,7 @@
|
||||
|
||||
add_library(unit_tests_static
|
||||
cgs_test.cpp
|
||||
custom_rep_test.cpp
|
||||
custom_rep_test_min_req.cpp
|
||||
custom_unit_test.cpp
|
||||
data_test.cpp
|
||||
dimension_op_test.cpp
|
||||
|
||||
+86
-47
@@ -32,37 +32,48 @@ using namespace units;
|
||||
namespace {
|
||||
|
||||
template<typename T>
|
||||
struct arithmetic_ops {
|
||||
// constexpr T& operator+=(T other) { value_ += other.value_; return *this; }
|
||||
// constexpr T& operator-=(T other) { value_ -= other.value_; return *this; }
|
||||
// constexpr T& operator*=(T other) { value_ *= other.value_; return *this; }
|
||||
// constexpr T& operator/=(T other) { value_ /= other.value_; return *this; }
|
||||
struct equality_ops {
|
||||
[[nodiscard]] friend constexpr bool operator==(T lhs, T rhs) { return lhs.value_ == rhs.value_; }
|
||||
[[nodiscard]] friend constexpr bool operator!=(T lhs, T rhs) { return !(lhs == rhs); }
|
||||
};
|
||||
|
||||
// [[nodiscard]] constexpr T operator-() const { return T(-value_); }
|
||||
|
||||
[[nodiscard]] friend constexpr T operator+(T lhs, T rhs) {
|
||||
return T(lhs.value_ + rhs.value_);
|
||||
}
|
||||
[[nodiscard]] friend constexpr T operator-(T lhs, T rhs) {
|
||||
return T(lhs.value_ - rhs.value_);
|
||||
}
|
||||
template<typename T>
|
||||
struct scaling_ops {
|
||||
[[nodiscard]] friend constexpr T operator*(T lhs, T rhs) {
|
||||
return T(lhs.value_ * rhs.value_);
|
||||
}
|
||||
[[nodiscard]] friend constexpr T operator/(T lhs, T rhs) {
|
||||
return T(lhs.value_ / rhs.value_);
|
||||
}
|
||||
|
||||
[[nodiscard]] friend constexpr bool operator==(T lhs, T rhs) { return lhs.value_ == rhs.value_; }
|
||||
[[nodiscard]] friend constexpr bool operator!=(T lhs, T rhs) { return !(lhs == rhs); }
|
||||
[[nodiscard]] friend constexpr bool operator<(T lhs, T rhs) { return lhs.value_ < rhs.value_; }
|
||||
[[nodiscard]] friend constexpr bool operator>(T lhs, T rhs) { return rhs < lhs; }
|
||||
[[nodiscard]] friend constexpr bool operator<=(T lhs, T rhs) { return !(rhs < lhs); }
|
||||
[[nodiscard]] friend constexpr bool operator>=(T lhs, T rhs) { return !(lhs < rhs); }
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
struct impl_constructible_impl_convertible : arithmetic_ops<impl_constructible_impl_convertible<T>> {
|
||||
struct scalar_ops : equality_ops<T>, scaling_ops<T> {};
|
||||
|
||||
template<typename T>
|
||||
struct impl_constructible : scalar_ops<impl_constructible<T>> {
|
||||
T value_{};
|
||||
impl_constructible() = default;
|
||||
constexpr impl_constructible(T v) : value_(std::move(v)) {}
|
||||
// no conversion to fundamental arithmetic types
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
using impl = impl_constructible<T>;
|
||||
|
||||
template<typename T>
|
||||
struct expl_constructible : scalar_ops<expl_constructible<T>> {
|
||||
T value_{};
|
||||
expl_constructible() = default;
|
||||
constexpr expl_constructible(T v) : value_(std::move(v)) {}
|
||||
// no conversion to fundamental arithmetic types
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
using expl = expl_constructible<T>;
|
||||
|
||||
template<typename T>
|
||||
struct impl_constructible_impl_convertible : scalar_ops<impl_constructible_impl_convertible<T>> /*, int_scaling_ops<impl_constructible_impl_convertible<T>> */ {
|
||||
T value_{};
|
||||
impl_constructible_impl_convertible() = default;
|
||||
constexpr impl_constructible_impl_convertible(T v) : value_(std::move(v)) {}
|
||||
@@ -77,7 +88,7 @@ static_assert(std::convertible_to<impl_impl<float>, float>);
|
||||
static_assert(units::Scalar<impl_impl<float>>);
|
||||
|
||||
template<typename T>
|
||||
struct expl_constructible_impl_convertible : arithmetic_ops<expl_constructible_impl_convertible<T>> {
|
||||
struct expl_constructible_impl_convertible : scalar_ops<expl_constructible_impl_convertible<T>> {
|
||||
T value_{};
|
||||
expl_constructible_impl_convertible() = default;
|
||||
constexpr explicit expl_constructible_impl_convertible(T v) : value_(std::move(v)) {}
|
||||
@@ -92,7 +103,7 @@ static_assert(std::convertible_to<expl_impl<float>, float>);
|
||||
static_assert(units::Scalar<expl_impl<float>>);
|
||||
|
||||
template<typename T>
|
||||
struct impl_constructible_expl_convertible : arithmetic_ops<impl_constructible_expl_convertible<T>> {
|
||||
struct impl_constructible_expl_convertible : scalar_ops<impl_constructible_expl_convertible<T>> {
|
||||
T value_{};
|
||||
impl_constructible_expl_convertible() = default;
|
||||
constexpr impl_constructible_expl_convertible(T v) : value_(std::move(v)) {}
|
||||
@@ -107,7 +118,7 @@ static_assert(!std::convertible_to<impl_expl<float>, float>);
|
||||
static_assert(units::Scalar<impl_expl<float>>);
|
||||
|
||||
template<typename T>
|
||||
struct expl_constructible_expl_convertible : arithmetic_ops<expl_constructible_expl_convertible<T>> {
|
||||
struct expl_constructible_expl_convertible : scalar_ops<expl_constructible_expl_convertible<T>> {
|
||||
T value_{};
|
||||
expl_constructible_expl_convertible() = default;
|
||||
constexpr explicit expl_constructible_expl_convertible(T v) : value_(std::move(v)) {}
|
||||
@@ -125,6 +136,12 @@ static_assert(units::Scalar<expl_expl<float>>);
|
||||
|
||||
namespace units {
|
||||
|
||||
template<typename T>
|
||||
inline constexpr bool treat_as_floating_point<impl<T>> = std::is_floating_point_v<T>;
|
||||
|
||||
template<typename T>
|
||||
inline constexpr bool treat_as_floating_point<expl_constructible<T>> = std::is_floating_point_v<T>;
|
||||
|
||||
template<typename T>
|
||||
inline constexpr bool treat_as_floating_point<impl_impl<T>> = std::is_floating_point_v<T>;
|
||||
|
||||
@@ -138,27 +155,14 @@ template<typename T>
|
||||
inline constexpr bool treat_as_floating_point<expl_expl<T>> = std::is_floating_point_v<T>;
|
||||
|
||||
template<typename T>
|
||||
struct quantity_values<impl_impl<T>> {
|
||||
static constexpr impl_impl<T> zero() { return impl_impl<T>(0); }
|
||||
static constexpr impl_impl<T> max() { return std::numeric_limits<T>::max(); }
|
||||
static constexpr impl_impl<T> min() { return std::numeric_limits<T>::lowest(); }
|
||||
struct quantity_values<impl<T>> {
|
||||
static constexpr impl<T> zero() { return 0; }
|
||||
static constexpr impl<T> max() { return std::numeric_limits<T>::max(); }
|
||||
static constexpr impl<T> min() { return std::numeric_limits<T>::lowest(); }
|
||||
};
|
||||
|
||||
} // namespace units
|
||||
|
||||
namespace std {
|
||||
|
||||
// template<typename T, typename U>
|
||||
// struct common_type<my_value<T>, my_value<U>> : std::type_identity<my_value<common_type_t<T, U>>> {};
|
||||
|
||||
// template<typename T, typename U>
|
||||
// struct common_type<my_value<T>, U> : common_type<T, U> {};
|
||||
|
||||
// template<typename T, typename U>
|
||||
// struct common_type<T, my_value<U>> : common_type<T, U> {};
|
||||
|
||||
} // namespace std
|
||||
|
||||
namespace {
|
||||
|
||||
using namespace units::si;
|
||||
@@ -225,15 +229,50 @@ static_assert(length<metre, impl_expl<double>>(length<metre, int>(1)).count() ==
|
||||
|
||||
// unit conversions
|
||||
|
||||
static_assert(length<metre, impl<int>>(length<kilometre, impl<int>>(1)).count() == impl<int>(1000));
|
||||
static_assert(length<metre, expl<int>>(length<kilometre, expl<int>>(expl(1))).count() == expl<int>(1000));
|
||||
static_assert(length<metre, impl_impl<int>>(length<kilometre, impl_impl<int>>(1)).count() == impl_impl<int>(1000));
|
||||
static_assert(length<metre, impl_expl<int>>(length<kilometre, impl_expl<int>>(1)).count() == impl_expl<int>(1000));
|
||||
static_assert(length<metre, expl_impl<int>>(length<kilometre, expl_impl<int>>(expl_impl(1))).count() == expl_impl<int>(1000));
|
||||
static_assert(length<metre, expl_expl<int>>(length<kilometre, expl_expl<int>>(expl_expl(1))).count() == expl_expl<int>(1000));
|
||||
|
||||
// static_assert(length<kilometre, impl<int>>(length<metre, impl<int>>(2000)).count() == impl<int>(2)); // should not compile (truncating conversion)
|
||||
static_assert(length<kilometre, impl<int>>(quantity_cast<kilometre>(length<metre, impl<int>>(2000))).count() == impl<int>(2));
|
||||
// static_assert(length<kilometre, expl<int>>(length<metre, expl<int>>(expl<int>(2000))).count() == expl<int>(2)); // should not compile (truncating conversion)
|
||||
static_assert(length<kilometre, expl<int>>(quantity_cast<kilometre>(length<metre, expl<int>>(expl<int>(2000)))).count() == expl<int>(2));
|
||||
// static_assert(length<kilometre, impl_impl<int>>(length<metre, impl_impl<int>>(2000)).count() == impl_impl<int>(2)); // should not compile (truncating conversion)
|
||||
static_assert(length<kilometre, impl_impl<int>>(quantity_cast<kilometre>(length<metre, impl_impl<int>>(2000))).count() == impl_impl<int>(2));
|
||||
// static_assert(length<kilometre, impl_expl<int>>(length<metre, impl_expl<int>>(2000)).count() == impl_expl<int>(2)); // should not compile (truncating conversion)
|
||||
static_assert(length<kilometre, impl_expl<int>>(quantity_cast<kilometre>(length<metre, impl_expl<int>>(2000))).count() == impl_expl<int>(2));
|
||||
// static_assert(length<kilometre, expl_impl<int>>(length<metre, expl_impl<int>>(expl_impl<int>(2000))).count() == expl_impl<int>(2)); // should not compile (truncating conversion)
|
||||
static_assert(length<kilometre, expl_impl<int>>(quantity_cast<kilometre>(length<metre, expl_impl<int>>(expl_impl<int>(2000)))).count() == expl_impl<int>(2));
|
||||
// static_assert(length<kilometre, expl_expl<int>>(length<metre, expl_expl<int>>(expl_expl<int>(2000))).count() == expl_expl<int>(2)); // should not compile (truncating conversion)
|
||||
static_assert(length<kilometre, expl_expl<int>>(quantity_cast<kilometre>(length<metre, expl_expl<int>>(expl_expl<int>(2000)))).count() == expl_expl<int>(2));
|
||||
|
||||
static_assert(length<metre, impl_impl<int>>::zero().count() == impl_impl<int>{0});
|
||||
static_assert(length<metre, impl_impl<int>>::min().count() == impl_impl<int>{std::numeric_limits<int>::lowest()});
|
||||
static_assert(length<metre, impl_impl<int>>::max().count() == impl_impl<int>{std::numeric_limits<int>::max()});
|
||||
static_assert(length<metre, impl_impl<double>>::zero().count() == impl_impl<double>{0.0});
|
||||
static_assert(length<metre, impl_impl<double>>::min().count() == impl_impl<double>{std::numeric_limits<double>::lowest()});
|
||||
static_assert(length<metre, impl_impl<double>>::max().count() == impl_impl<double>{std::numeric_limits<double>::max()});
|
||||
// static_assert(velocity<metre_per_second, impl<int>>(velocity<kilometre_per_hour, impl<int>>(72)).count() == impl<int>(20)); // should not compile (truncating conversion)
|
||||
static_assert(velocity<metre_per_second, impl<int>>(quantity_cast<metre_per_second>(velocity<kilometre_per_hour, impl<int>>(72))).count() == impl<int>(20));
|
||||
// static_assert(velocity<metre_per_second, expl<int>>(velocity<kilometre_per_hour, expl<int>>(expl(72))).count() == expl<int>(20)); // should not compile (truncating conversion)
|
||||
static_assert(velocity<metre_per_second, expl<int>>(quantity_cast<metre_per_second>(velocity<kilometre_per_hour, expl<int>>(expl(72)))).count() == expl<int>(20));
|
||||
// static_assert(velocity<metre_per_second, impl_impl<int>>(velocity<kilometre_per_hour, impl_impl<int>>(72)).count() == impl_impl<int>(20)); // should not compile (truncating conversion)
|
||||
static_assert(velocity<metre_per_second, impl_impl<int>>(quantity_cast<metre_per_second>(velocity<kilometre_per_hour, impl_impl<int>>(72))).count() == impl_impl<int>(20));
|
||||
// static_assert(velocity<metre_per_second, impl_expl<int>>(velocity<kilometre_per_hour, impl_expl<int>>(72)).count() == impl_expl<int>(20)); // should not compile (truncating conversion)
|
||||
static_assert(velocity<metre_per_second, impl_expl<int>>(quantity_cast<metre_per_second>(velocity<kilometre_per_hour, impl_expl<int>>(72))).count() == impl_expl<int>(20));
|
||||
// static_assert(velocity<metre_per_second, expl_impl<int>>(velocity<kilometre_per_hour, expl_impl<int>>(expl_impl(72))).count() == expl_impl<int>(20)); // should not compile (truncating conversion)
|
||||
static_assert(velocity<metre_per_second, expl_impl<int>>(quantity_cast<metre_per_second>(velocity<kilometre_per_hour, expl_impl<int>>(expl_impl(72)))).count() == expl_impl<int>(20));
|
||||
// static_assert(velocity<metre_per_second, expl_expl<int>>(velocity<kilometre_per_hour, expl_expl<int>>(expl_expl(72))).count() == expl_expl<int>(20)); // should not compile (truncating conversion)
|
||||
static_assert(velocity<metre_per_second, expl_expl<int>>(quantity_cast<metre_per_second>(velocity<kilometre_per_hour, expl_expl<int>>(expl_expl(72)))).count() == expl_expl<int>(20));
|
||||
|
||||
// static_assert(velocity<kilometre_per_hour, impl<int>>(velocity<metre_per_second, impl<int>>(20)).count() == impl<int>(72)); // should not compile (truncating conversion)
|
||||
static_assert(velocity<kilometre_per_hour, impl<int>>(quantity_cast<kilometre_per_hour>(velocity<metre_per_second, impl<int>>(20))).count() == impl<int>(72));
|
||||
// static_assert(velocity<kilometre_per_hour, expl<int>>(velocity<metre_per_second, expl<int>>(expl<int>(20))).count() == expl<int>(72)); // should not compile (truncating conversion)
|
||||
static_assert(velocity<kilometre_per_hour, expl<int>>(quantity_cast<kilometre_per_hour>(velocity<metre_per_second, expl<int>>(expl<int>(20)))).count() == expl<int>(72));
|
||||
// static_assert(velocity<kilometre_per_hour, impl_impl<int>>(velocity<metre_per_second, impl_impl<int>>(20)).count() == impl_impl<int>(72)); // should not compile (truncating conversion)
|
||||
static_assert(velocity<kilometre_per_hour, impl_impl<int>>(quantity_cast<kilometre_per_hour>(velocity<metre_per_second, impl_impl<int>>(20))).count() == impl_impl<int>(72));
|
||||
// static_assert(velocity<kilometre_per_hour, impl_expl<int>>(velocity<metre_per_second, impl_expl<int>>(20)).count() == impl_expl<int>(72)); // should not compile (truncating conversion)
|
||||
static_assert(velocity<kilometre_per_hour, impl_expl<int>>(quantity_cast<kilometre_per_hour>(velocity<metre_per_second, impl_expl<int>>(20))).count() == impl_expl<int>(72));
|
||||
// static_assert(velocity<kilometre_per_hour, expl_impl<int>>(velocity<metre_per_second, expl_impl<int>>(expl_impl<int>(20))).count() == expl_impl<int>(72)); // should not compile (truncating conversion)
|
||||
static_assert(velocity<kilometre_per_hour, expl_impl<int>>(quantity_cast<kilometre_per_hour>(velocity<metre_per_second, expl_impl<int>>(expl_impl<int>(20)))).count() == expl_impl<int>(72));
|
||||
// static_assert(velocity<kilometre_per_hour, expl_expl<int>>(velocity<metre_per_second, expl_expl<int>>(expl_expl<int>(20))).count() == expl_expl<int>(72)); // should not compile (truncating conversion)
|
||||
static_assert(velocity<kilometre_per_hour, expl_expl<int>>(quantity_cast<kilometre_per_hour>(velocity<metre_per_second, expl_expl<int>>(expl_expl<int>(20)))).count() == expl_expl<int>(72));
|
||||
|
||||
} // namespace
|
||||
Reference in new issue
Block a user