mirror of
https://github.com/mpusz/mp-units.git
synced 2025-08-01 03:14:29 +02:00
Number concept used for representation
This commit is contained in:
@@ -30,6 +30,22 @@ namespace units {
|
||||
|
||||
using namespace mp::std_concepts; // todo Remove when std::concepts will arrive
|
||||
|
||||
template<typename T>
|
||||
concept bool Number = requires(T a, T b) {
|
||||
{ a + b} -> T;
|
||||
{ a - b } -> T;
|
||||
{ a * b } -> T;
|
||||
{ a / b } -> T;
|
||||
{ -a } -> T;
|
||||
{ a += b } -> T&;
|
||||
{ a -= b } -> T&;
|
||||
{ a *= b } -> T&;
|
||||
{ a /= b } -> T&;
|
||||
{ T{0} };// can construct a T from a zero
|
||||
// …
|
||||
} ;
|
||||
|
||||
|
||||
// static_sign
|
||||
|
||||
template<std::intmax_t Pn>
|
||||
|
@@ -29,8 +29,9 @@ namespace units {
|
||||
|
||||
// is_quantity
|
||||
|
||||
template<Dimension D, Unit U, typename Rep>
|
||||
requires Same<D, typename U::dimension> class quantity;
|
||||
template<Dimension D, Unit U, Number Rep>
|
||||
requires Same<D, typename U::dimension>
|
||||
class quantity;
|
||||
|
||||
namespace detail {
|
||||
|
||||
@@ -38,7 +39,7 @@ namespace units {
|
||||
struct is_quantity : std::false_type {
|
||||
};
|
||||
|
||||
template<Dimension D, Unit U, typename Rep>
|
||||
template<Dimension D, Unit U, Number Rep>
|
||||
struct is_quantity<quantity<D, U, Rep>> : std::true_type {
|
||||
};
|
||||
|
||||
@@ -60,38 +61,38 @@ namespace units {
|
||||
|
||||
namespace detail {
|
||||
|
||||
template<Quantity To, Ratio CR, typename CRep, bool NumIsOne = false, bool DenIsOne = false>
|
||||
template<Quantity To, Ratio CR, Number CRep, bool NumIsOne = false, bool DenIsOne = false>
|
||||
struct quantity_cast_impl {
|
||||
template<Dimension D, Ratio R, typename Rep>
|
||||
static constexpr To cast(const quantity<D, unit<D, R>, Rep>& q)
|
||||
template<Quantity Q>
|
||||
static constexpr To cast(const Q& q)
|
||||
{
|
||||
return To(static_cast<typename To::rep>(static_cast<CRep>(q.count()) * static_cast<CRep>(CR::num) /
|
||||
static_cast<CRep>(CR::den)));
|
||||
}
|
||||
};
|
||||
|
||||
template<Quantity To, Ratio CR, typename CRep>
|
||||
template<Quantity To, Ratio CR, Number CRep>
|
||||
struct quantity_cast_impl<To, CR, CRep, true, true> {
|
||||
template<Dimension D, Ratio R, typename Rep>
|
||||
static constexpr To cast(const quantity<D, unit<D, R>, Rep>& q)
|
||||
template<Quantity Q>
|
||||
static constexpr To cast(const Q& q)
|
||||
{
|
||||
return To(static_cast<typename To::rep>(q.count()));
|
||||
}
|
||||
};
|
||||
|
||||
template<Quantity To, Ratio CR, typename CRep>
|
||||
template<Quantity To, Ratio CR, Number CRep>
|
||||
struct quantity_cast_impl<To, CR, CRep, true, false> {
|
||||
template<Dimension D, Ratio R, typename Rep>
|
||||
static constexpr To cast(const quantity<D, unit<D, R>, Rep>& q)
|
||||
template<Quantity Q>
|
||||
static constexpr To cast(const Q& q)
|
||||
{
|
||||
return To(static_cast<typename To::rep>(static_cast<CRep>(q.count()) / static_cast<CRep>(CR::den)));
|
||||
}
|
||||
};
|
||||
|
||||
template<Quantity To, Ratio CR, typename CRep>
|
||||
template<Quantity To, Ratio CR, Number CRep>
|
||||
struct quantity_cast_impl<To, CR, CRep, false, true> {
|
||||
template<Dimension D, Ratio R, typename Rep>
|
||||
static constexpr To cast(const quantity<D, unit<D, R>, Rep>& q)
|
||||
template<Quantity Q>
|
||||
static constexpr To cast(const Q& q)
|
||||
{
|
||||
return To(static_cast<typename To::rep>(static_cast<CRep>(q.count()) * static_cast<CRep>(CR::num)));
|
||||
}
|
||||
@@ -99,8 +100,9 @@ namespace units {
|
||||
|
||||
} // namespace detail
|
||||
|
||||
template<Quantity To, Dimension D, Unit U, typename Rep>
|
||||
requires Same<typename To::dimension, D> constexpr To quantity_cast(const quantity<D, U, Rep>& q)
|
||||
template<Quantity To, Dimension D, Unit U, Number Rep>
|
||||
requires Same<typename To::dimension, D> constexpr
|
||||
To quantity_cast(const quantity<D, U, Rep>& q)
|
||||
{
|
||||
using c_ratio = std::ratio_divide<typename U::ratio, typename To::unit::ratio>;
|
||||
using c_rep = std::common_type_t<typename To::rep, Rep, intmax_t>;
|
||||
@@ -110,7 +112,7 @@ namespace units {
|
||||
|
||||
// quantity_values
|
||||
|
||||
template<typename Rep>
|
||||
template<Number Rep>
|
||||
struct quantity_values {
|
||||
static constexpr Rep zero() { return Rep(0); }
|
||||
static constexpr Rep max() { return std::numeric_limits<Rep>::max(); }
|
||||
@@ -119,8 +121,9 @@ namespace units {
|
||||
|
||||
// quantity
|
||||
|
||||
template<Dimension D, Unit U, typename Rep>
|
||||
requires Same<D, typename U::dimension> class quantity {
|
||||
template<Dimension D, Unit U, Number Rep>
|
||||
requires Same<D, typename U::dimension>
|
||||
class quantity {
|
||||
Rep value_;
|
||||
|
||||
public:
|
||||
@@ -135,8 +138,8 @@ namespace units {
|
||||
|
||||
template<class Rep2>
|
||||
requires ConvertibleTo<Rep2, rep> &&
|
||||
(treat_as_floating_point_v<rep> || !treat_as_floating_point_v<Rep2>)constexpr explicit quantity(const Rep2& r)
|
||||
: value_{static_cast<rep>(r)}
|
||||
(treat_as_floating_point_v<rep> || !treat_as_floating_point_v<Rep2>)
|
||||
constexpr explicit quantity(const Rep2& r) : value_{static_cast<rep>(r)}
|
||||
{
|
||||
}
|
||||
|
||||
@@ -144,8 +147,8 @@ namespace units {
|
||||
requires Same<dimension, typename Q2::dimension>&& ConvertibleTo<typename Q2::rep, rep> &&
|
||||
(treat_as_floating_point_v<rep> ||
|
||||
(std::ratio_divide<typename Q2::unit::ratio, typename unit::ratio>::den == 1 &&
|
||||
!treat_as_floating_point_v<typename Q2::rep>)) constexpr quantity(const Q2& q)
|
||||
: value_{quantity_cast<quantity>(q).count()}
|
||||
!treat_as_floating_point_v<typename Q2::rep>))
|
||||
constexpr quantity(const Q2& q) : value_{quantity_cast<quantity>(q).count()}
|
||||
{
|
||||
}
|
||||
|
||||
@@ -212,7 +215,7 @@ namespace units {
|
||||
};
|
||||
|
||||
// clang-format off
|
||||
template<Dimension D, Unit U1, typename Rep1, Unit U2, typename Rep2>
|
||||
template<Dimension D, Unit U1, Number Rep1, Unit U2, Number Rep2>
|
||||
std::common_type_t<quantity<D, U1, Rep1>, quantity<D, U2, Rep2>>
|
||||
constexpr operator+(const quantity<D, U1, Rep1>& lhs,
|
||||
const quantity<D, U2, Rep2>& rhs)
|
||||
@@ -221,7 +224,7 @@ namespace units {
|
||||
return ret(ret(lhs).count() + ret(rhs).count());
|
||||
}
|
||||
|
||||
template<Dimension D, Unit U1, typename Rep1, Unit U2, typename Rep2>
|
||||
template<Dimension D, Unit U1, Number Rep1, Unit U2, Number Rep2>
|
||||
std::common_type_t<quantity<D, U1, Rep1>, quantity<D, U2, Rep2>>
|
||||
constexpr operator-(const quantity<D, U1, Rep1>& lhs,
|
||||
const quantity<D, U2, Rep2>& rhs)
|
||||
@@ -230,7 +233,7 @@ namespace units {
|
||||
return ret(ret(lhs).count() - ret(rhs).count());
|
||||
}
|
||||
|
||||
template<Dimension D, Unit U, typename Rep1, typename Rep2>
|
||||
template<Dimension D, Unit U, Number Rep1, Number Rep2>
|
||||
quantity<D, U, std::common_type_t<Rep1, Rep2>>
|
||||
constexpr operator*(const quantity<D, U, Rep1>& q,
|
||||
const Rep2& v)
|
||||
@@ -239,7 +242,7 @@ namespace units {
|
||||
return ret(ret(q).count() * v);
|
||||
}
|
||||
|
||||
template<typename Rep1, Dimension D, Unit U, typename Rep2>
|
||||
template<Number Rep1, Dimension D, Unit U, Number Rep2>
|
||||
quantity<D, U, std::common_type_t<Rep1, Rep2>>
|
||||
constexpr operator*(const Rep1& v,
|
||||
const quantity<D, U, Rep2>& q)
|
||||
@@ -247,7 +250,7 @@ namespace units {
|
||||
return q * v;
|
||||
}
|
||||
|
||||
template<Dimension D1, Unit U1, typename Rep1, Dimension D2, Unit U2, typename Rep2>
|
||||
template<Dimension D1, Unit U1, Number Rep1, Dimension D2, Unit U2, Number Rep2>
|
||||
requires treat_as_floating_point_v<std::common_type_t<Rep1, Rep2>> || std::ratio_multiply<typename U1::ratio, typename U2::ratio>::den == 1
|
||||
quantity<dimension_multiply_t<D1, D2>, unit<dimension_multiply_t<D1, D2>, std::ratio_multiply<typename U1::ratio, typename U2::ratio>>, std::common_type_t<Rep1, Rep2>>
|
||||
constexpr operator*(const quantity<D1, U1, Rep1>& lhs,
|
||||
@@ -258,7 +261,7 @@ namespace units {
|
||||
return ret(lhs.count() * rhs.count());
|
||||
}
|
||||
|
||||
template<typename Rep1, Exponent... E, Unit U, typename Rep2>
|
||||
template<Number Rep1, Exponent... E, Unit U, Number Rep2>
|
||||
quantity<dimension<exp_invert_t<E>...>, unit<dimension<exp_invert_t<E>...>, std::ratio<U::ratio::den, U::ratio::num>>, std::common_type_t<Rep1, Rep2>>
|
||||
constexpr operator/(const Rep1& v,
|
||||
const quantity<dimension<E...>, U, Rep2>& q)
|
||||
@@ -269,7 +272,7 @@ namespace units {
|
||||
return ret(v / den(q).count());
|
||||
}
|
||||
|
||||
template<Dimension D, Unit U, typename Rep1, typename Rep2>
|
||||
template<Dimension D, Unit U, Number Rep1, Number Rep2>
|
||||
quantity<D, U, std::common_type_t<Rep1, Rep2>>
|
||||
constexpr operator/(const quantity<D, U, Rep1>& q,
|
||||
const Rep2& v)
|
||||
@@ -278,7 +281,7 @@ namespace units {
|
||||
return ret(ret(q).count() / v);
|
||||
}
|
||||
|
||||
template<Dimension D, Unit U1, typename Rep1, Unit U2, typename Rep2>
|
||||
template<Dimension D, Unit U1, Number Rep1, Unit U2, Number Rep2>
|
||||
std::common_type_t<Rep1, Rep2>
|
||||
constexpr operator/(const quantity<D, U1, Rep1>& lhs,
|
||||
const quantity<D, U2, Rep2>& rhs)
|
||||
@@ -287,7 +290,7 @@ namespace units {
|
||||
return cq(lhs).count() / cq(rhs).count();
|
||||
}
|
||||
|
||||
template<Dimension D1, Unit U1, typename Rep1, Dimension D2, Unit U2, typename Rep2>
|
||||
template<Dimension D1, Unit U1, Number Rep1, Dimension D2, Unit U2, Number Rep2>
|
||||
requires treat_as_floating_point_v<std::common_type_t<Rep1, Rep2>> || std::ratio_divide<typename U1::ratio, typename U2::ratio>::den == 1
|
||||
quantity<dimension_divide_t<D1, D2>, unit<dimension_divide_t<D1, D2>, std::ratio_divide<typename U1::ratio, typename U2::ratio>>, std::common_type_t<Rep1, Rep2>>
|
||||
constexpr operator/(const quantity<D1, U1, Rep1>& lhs,
|
||||
@@ -298,7 +301,7 @@ namespace units {
|
||||
return ret(lhs.count() / rhs.count());
|
||||
}
|
||||
|
||||
template<Dimension D, Unit U, typename Rep1, typename Rep2>
|
||||
template<Dimension D, Unit U, Number Rep1, Number Rep2>
|
||||
quantity<D, U, std::common_type_t<Rep1, Rep2>>
|
||||
constexpr operator%(const quantity<D, U, Rep1>& q,
|
||||
const Rep2& v)
|
||||
@@ -307,7 +310,7 @@ namespace units {
|
||||
return ret(ret(q).count() % v);
|
||||
}
|
||||
|
||||
template<Dimension D, Unit U1, typename Rep1, Unit U2, typename Rep2>
|
||||
template<Dimension D, Unit U1, Number Rep1, Unit U2, Number Rep2>
|
||||
std::common_type_t<quantity<D, U1, Rep1>, quantity<D, U2, Rep2>>
|
||||
constexpr operator%(const quantity<D, U1, Rep1>& lhs,
|
||||
const quantity<D, U2, Rep2>& rhs)
|
||||
@@ -318,39 +321,39 @@ namespace units {
|
||||
|
||||
// clang-format on
|
||||
|
||||
template<Dimension D, Unit U1, typename Rep1, Unit U2, typename Rep2>
|
||||
template<Dimension D, Unit U1, Number Rep1, Unit U2, Number Rep2>
|
||||
constexpr bool operator==(const quantity<D, U1, Rep1>& lhs, const quantity<D, U2, Rep2>& rhs)
|
||||
{
|
||||
using ct = std::common_type_t<quantity<D, U1, Rep1>, quantity<D, U2, Rep2>>;
|
||||
return ct(lhs).count() == ct(rhs).count();
|
||||
}
|
||||
|
||||
template<Dimension D, Unit U1, typename Rep1, Unit U2, typename Rep2>
|
||||
template<Dimension D, Unit U1, Number Rep1, Unit U2, Number Rep2>
|
||||
constexpr bool operator!=(const quantity<D, U1, Rep1>& lhs, const quantity<D, U2, Rep2>& rhs)
|
||||
{
|
||||
return !(lhs == rhs);
|
||||
}
|
||||
|
||||
template<Dimension D, Unit U1, typename Rep1, Unit U2, typename Rep2>
|
||||
template<Dimension D, Unit U1, Number Rep1, Unit U2, Number Rep2>
|
||||
constexpr bool operator<(const quantity<D, U1, Rep1>& lhs, const quantity<D, U2, Rep2>& rhs)
|
||||
{
|
||||
using ct = std::common_type_t<quantity<D, U1, Rep1>, quantity<D, U2, Rep2>>;
|
||||
return ct(lhs).count() < ct(rhs).count();
|
||||
}
|
||||
|
||||
template<Dimension D, Unit U1, typename Rep1, Unit U2, typename Rep2>
|
||||
template<Dimension D, Unit U1, Number Rep1, Unit U2, Number Rep2>
|
||||
constexpr bool operator<=(const quantity<D, U1, Rep1>& lhs, const quantity<D, U2, Rep2>& rhs)
|
||||
{
|
||||
return !(rhs < lhs);
|
||||
}
|
||||
|
||||
template<Dimension D, Unit U1, typename Rep1, Unit U2, typename Rep2>
|
||||
template<Dimension D, Unit U1, Number Rep1, Unit U2, Number Rep2>
|
||||
constexpr bool operator>(const quantity<D, U1, Rep1>& lhs, const quantity<D, U2, Rep2>& rhs)
|
||||
{
|
||||
return rhs < lhs;
|
||||
}
|
||||
|
||||
template<Dimension D, Unit U1, typename Rep1, Unit U2, typename Rep2>
|
||||
template<Dimension D, Unit U1, Number Rep1, Unit U2, Number Rep2>
|
||||
constexpr bool operator>=(const quantity<D, U1, Rep1>& lhs, const quantity<D, U2, Rep2>& rhs)
|
||||
{
|
||||
return !(lhs < rhs);
|
||||
@@ -360,8 +363,13 @@ namespace units {
|
||||
|
||||
namespace std {
|
||||
|
||||
template<units::Dimension D, units::Unit U, units::Number Rep1, units::Number Rep2>
|
||||
struct common_type<units::quantity<D, U, Rep1>, units::quantity<D, U, Rep2>> {
|
||||
using type = units::quantity<D, U, std::common_type_t<Rep1, Rep2>>;
|
||||
};
|
||||
|
||||
// todo: simplified
|
||||
template<units::Dimension D, units::Unit U1, typename Rep1, units::Unit U2, typename Rep2>
|
||||
template<units::Dimension D, units::Unit U1, units::Number Rep1, units::Unit U2, units::Number Rep2>
|
||||
struct common_type<units::quantity<D, U1, Rep1>, units::quantity<D, U2, Rep2>> {
|
||||
using type = units::quantity<D, units::unit<D, units::common_ratio_t<typename U1::ratio, typename U2::ratio>>,
|
||||
std::common_type_t<Rep1, Rep2>>;
|
||||
|
@@ -23,6 +23,7 @@
|
||||
#include "units/si/velocity.h"
|
||||
#include <utility>
|
||||
#include <chrono>
|
||||
|
||||
using namespace units;
|
||||
using namespace units::literals;
|
||||
|
||||
@@ -30,12 +31,17 @@ namespace {
|
||||
|
||||
template<typename T>
|
||||
class my_value {
|
||||
T data_{};
|
||||
T value_{};
|
||||
|
||||
public:
|
||||
my_value() = default;
|
||||
constexpr my_value(T v) : data_{v} {}
|
||||
constexpr operator T() const { return data_; }
|
||||
constexpr my_value(T v) : value_{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; }
|
||||
constexpr operator const T&() const { return value_; }
|
||||
constexpr operator T&() { return value_; }
|
||||
};
|
||||
|
||||
} // namespace
|
||||
@@ -74,15 +80,15 @@ namespace {
|
||||
|
||||
// class invariants
|
||||
|
||||
// constexpr quantity<dimension_length, second> q; // should a static_assert
|
||||
// constexpr quantity<length<meter, int>> error(0_m); // should trigger a static_assert
|
||||
// constexpr quantity<int, float> error(0); // should trigger a static_assert
|
||||
// constexpr quantity<int, std::ratio<-1, 1>> error(0); // should trigger a static_assert
|
||||
// constexpr quantity<dimension_length, second, int> q; // should a static_assert
|
||||
// constexpr quantity<dimension_length, meter, length<meter, int>> error(0_m); // should trigger a static_assert
|
||||
// constexpr quantity<int, int, double> error(0); // should trigger a static_assert
|
||||
// constexpr quantity<dimension_length, unit<dimension_length, std::ratio<-1, 1>>, int> error(0); // should trigger a static_assert
|
||||
|
||||
// member types
|
||||
|
||||
static_assert(std::is_same_v<length<meter, int>::rep, int>);
|
||||
static_assert(std::is_same_v<length<meter, float>::rep, float>);
|
||||
static_assert(std::is_same_v<length<meter, double>::rep, double>);
|
||||
static_assert(std::is_same_v<length<meter, int>::unit, meter>);
|
||||
static_assert(std::is_same_v<length<kilometer, int>::unit, kilometer>);
|
||||
|
||||
@@ -94,30 +100,30 @@ namespace {
|
||||
static_assert(length<meter, int>(km).count() == km.count());
|
||||
|
||||
static_assert(length<meter, int>(1).count() == 1);
|
||||
static_assert(length<meter, int>(my_value<int>(1)).count() == 1);
|
||||
static_assert(length<meter, int>(my_value(1)).count() == 1);
|
||||
static_assert(length<meter, my_value<int>>(1).count() == 1);
|
||||
// static_assert(length<meter, int>(1.0).count() == 1); // should not compile
|
||||
// static_assert(length<meter, int>(my_value<float>(1.0)).count() == 1); // should not compile
|
||||
// static_assert(length<meter, my_value<int>>(1.0).count() == 1); // should not compile
|
||||
static_assert(length<meter, float>(1.0).count() == 1.0);
|
||||
static_assert(length<meter, float>(my_value<float>(1.0)).count() == 1.0);
|
||||
static_assert(length<meter, float>(1).count() == 1.0);
|
||||
static_assert(length<meter, float>(my_value<int>(1)).count() == 1.0);
|
||||
static_assert(length<meter, float>(3.14f).count() == 3.14f);
|
||||
static_assert(length<meter, my_value<float>>(1.0).count() == 1.0);
|
||||
static_assert(length<meter, my_value<float>>(1).count() == 1.0);
|
||||
static_assert(length<meter, my_value<float>>(3.14f).count() == 3.14f);
|
||||
// static_assert(length<meter, int>(my_value(1.0)).count() == 1); // should not compile
|
||||
// static_assert(length<meter, my_value>(1.0).count() == 1); // should not compile
|
||||
static_assert(length<meter, double>(1.0).count() == 1.0);
|
||||
static_assert(length<meter, double>(my_value(1.0)).count() == 1.0);
|
||||
static_assert(length<meter, double>(1).count() == 1.0);
|
||||
static_assert(length<meter, double>(my_value(1)).count() == 1.0);
|
||||
static_assert(length<meter, double>(3.14).count() == 3.14);
|
||||
static_assert(length<meter, my_value<double>>(1.0).count() == 1.0);
|
||||
static_assert(length<meter, my_value<double>>(1).count() == 1.0);
|
||||
static_assert(length<meter, my_value<double>>(3.14).count() == 3.14);
|
||||
|
||||
static_assert(length<meter, int>(km).count() == 1000);
|
||||
// static_assert(length<meter, int>(length<meter, float>(3.14)).count() == 3); // should not compile
|
||||
// static_assert(length<meter, int>(length<meter, double>(3.14)).count() == 3); // should not compile
|
||||
static_assert(length<meter, int>(quantity_cast<length<meter, int>>(3.14_m)).count() == 3);
|
||||
// static_assert(length<meter, int>(length<meter, my_value<float>>(1000.0)).count() == 1000); // should not compile
|
||||
// static_assert(length<meter, my_value<int>>(1000.0_m).count() == 1000); // should not compile
|
||||
static_assert(length<meter, float>(1000.0_m).count() == 1000.0);
|
||||
static_assert(length<meter, float>(length<meter, my_value<float>>(1000.0)).count() == 1000.0);
|
||||
static_assert(length<meter, my_value<float>>(1000.0_m).count() == 1000.0);
|
||||
static_assert(length<meter, float>(km).count() == 1000.0);
|
||||
static_assert(length<meter, my_value<float>>(km).count() == 1000.0);
|
||||
// static_assert(length<meter, int>(length<meter, my_value<double>>(1000.0)).count() == 1000); // should not compile
|
||||
// static_assert(length<meter, my_value>(1000.0_m).count() == 1000); // should not compile
|
||||
static_assert(length<meter, double>(1000.0_m).count() == 1000.0);
|
||||
static_assert(length<meter, double>(length<meter, my_value<double>>(1000.0)).count() == 1000.0);
|
||||
static_assert(length<meter, my_value<double>>(1000.0_m).count() == 1000.0);
|
||||
static_assert(length<meter, double>(km).count() == 1000.0);
|
||||
static_assert(length<meter, my_value<double>>(km).count() == 1000.0);
|
||||
static_assert(length<meter, int>(1_km).count() == 1000);
|
||||
// static_assert(length<meter, int>(1_s).count() == 1); // should not compile
|
||||
// static_assert(length<kilometer, int>(1010_m).count() == 1); // should not compile
|
||||
@@ -136,15 +142,15 @@ namespace {
|
||||
static_assert(length<meter, int>::zero().count() == 0);
|
||||
static_assert(length<meter, int>::min().count() == std::numeric_limits<int>::lowest());
|
||||
static_assert(length<meter, int>::max().count() == std::numeric_limits<int>::max());
|
||||
static_assert(length<meter, float>::zero().count() == 0.0);
|
||||
static_assert(length<meter, float>::min().count() == std::numeric_limits<float>::lowest());
|
||||
static_assert(length<meter, float>::max().count() == std::numeric_limits<float>::max());
|
||||
static_assert(length<meter, double>::zero().count() == 0.0);
|
||||
static_assert(length<meter, double>::min().count() == std::numeric_limits<double>::lowest());
|
||||
static_assert(length<meter, double>::max().count() == std::numeric_limits<double>::max());
|
||||
static_assert(length<meter, my_value<int>>::zero().count() == 0);
|
||||
static_assert(length<meter, my_value<int>>::min().count() == std::numeric_limits<int>::lowest());
|
||||
static_assert(length<meter, my_value<int>>::max().count() == std::numeric_limits<int>::max());
|
||||
static_assert(length<meter, my_value<float>>::zero().count() == 0.0);
|
||||
static_assert(length<meter, my_value<float>>::min().count() == std::numeric_limits<float>::lowest());
|
||||
static_assert(length<meter, my_value<float>>::max().count() == std::numeric_limits<float>::max());
|
||||
static_assert(length<meter, my_value<double>>::zero().count() == 0.0);
|
||||
static_assert(length<meter, my_value<double>>::min().count() == std::numeric_limits<double>::lowest());
|
||||
static_assert(length<meter, my_value<double>>::max().count() == std::numeric_limits<double>::max());
|
||||
|
||||
// unary member operators
|
||||
|
||||
@@ -183,14 +189,20 @@ namespace {
|
||||
|
||||
// non-member arithmetic operators
|
||||
|
||||
static_assert(std::is_same_v<decltype(length<meter, int>() + length<meter, float>()), length<meter, float>>);
|
||||
static_assert(std::is_same_v<decltype(length<meter, float>() - length<meter, int>()), length<meter, float>>);
|
||||
static_assert(std::is_same_v<decltype(length<meter, int>() * 1.0f), length<meter, float>>);
|
||||
static_assert(std::is_same_v<decltype(1.0f * length<meter, int>()), length<meter, float>>);
|
||||
static_assert(std::is_same_v<decltype(length<meter, int>() / 1.0f), length<meter, float>>);
|
||||
static_assert(std::is_same_v<decltype(length<meter, int>() / length<meter, float>()), float>);
|
||||
static_assert(std::is_same_v<decltype(length<meter, int>() % short(1)), length<meter, int>>);
|
||||
static_assert(std::is_same_v<decltype(length<meter, int>() % length<meter, short>(1)), length<meter, int>>);
|
||||
static_assert(std::is_same_v<decltype(length<meter, int>() + length<meter, double>()), quantity<dimension_length, meter, double>>);
|
||||
static_assert(std::is_same_v<decltype(length<meter, int>() + length<meter, double>()), quantity<dimension_length, meter, double>>);
|
||||
static_assert(std::is_same_v<decltype(length<kilometer, int>() + length<meter, double>()), quantity<dimension_length, unit<dimension_length, std::ratio<1>>, double>>);
|
||||
static_assert(std::is_same_v<decltype(length<meter, double>() - length<meter, int>()), quantity<dimension_length, meter, double>>);
|
||||
static_assert(std::is_same_v<decltype(length<kilometer, double>() - length<meter, int>()), quantity<dimension_length, unit<dimension_length, std::ratio<1>>, double>>);
|
||||
static_assert(std::is_same_v<decltype(length<meter, int>() * 1.0), quantity<dimension_length, meter, double>>);
|
||||
static_assert(std::is_same_v<decltype(1.0 * length<meter, int>()), quantity<dimension_length, meter, double>>);
|
||||
static_assert(std::is_same_v<decltype(velocity<meter_per_second, int>() * units::time<second, int>()), quantity<dimension_length, unit<dimension_length, std::ratio<1>>, int>>);
|
||||
static_assert(std::is_same_v<decltype(length<meter, int>() / 1.0), quantity<dimension_length, meter, double>>);
|
||||
static_assert(std::is_same_v<decltype(length<meter, int>() / length<meter, double>()), double>);
|
||||
static_assert(std::is_same_v<decltype(length<kilometer, int>() / length<meter, double>()), double>);
|
||||
static_assert(std::is_same_v<decltype(length<meter, int>() / units::time<second, int>()), quantity<dimension_velocity, meter_per_second, int>>);
|
||||
static_assert(std::is_same_v<decltype(length<meter, int>() % short(1)), quantity<dimension_length, meter, int>>);
|
||||
static_assert(std::is_same_v<decltype(length<meter, int>() % length<meter, short>(1)), quantity<dimension_length, meter, int>>);
|
||||
|
||||
static_assert((1_m + km).count() == 1001);
|
||||
static_assert((1_m + 1_km).count() == 1001);
|
||||
@@ -242,9 +254,9 @@ namespace {
|
||||
|
||||
// common_type
|
||||
|
||||
static_assert(std::is_same_v<std::common_type_t<length<meter, int>, length<kilometer, int>>, length<meter, int>>);
|
||||
static_assert(std::is_same_v<std::common_type_t<length<kilometer, long long>, length<meter, int>>, length<meter, long long>>);
|
||||
static_assert(std::is_same_v<std::common_type_t<length<kilometer, long long>, length<millimeter, float>>, length<millimeter, float>>);
|
||||
static_assert(std::is_same_v<std::common_type_t<length<meter, int>, length<kilometer, int>>, length<unit<dimension_length, std::ratio<1>>, int>>);
|
||||
static_assert(std::is_same_v<std::common_type_t<length<kilometer, long long>, length<meter, int>>, length<unit<dimension_length, std::ratio<1>>, long long>>);
|
||||
static_assert(std::is_same_v<std::common_type_t<length<kilometer, long long>, length<millimeter, double>>, length<unit<dimension_length, std::ratio<1, 1000>>, double>>);
|
||||
|
||||
// quantity_cast
|
||||
|
||||
|
@@ -48,7 +48,9 @@ namespace {
|
||||
static_assert(10_km / 5_km == 2);
|
||||
static_assert(10_km / 2 == 5_km);
|
||||
|
||||
static_assert(1_m == 100_cm)//static_assert(5_in + 8_cm == 207_mm);
|
||||
// static_assert(1_ft == 12_in);
|
||||
static_assert(1_m == 100_cm);
|
||||
// static_assert(5_in + 8_cm == 207_mm);
|
||||
|
||||
// velocity
|
||||
|
||||
|
Reference in New Issue
Block a user