mirror of
https://github.com/mpusz/mp-units.git
synced 2025-07-30 18:37:15 +02:00
Downcasting support added to quantity_cast
(resolves #26)
This commit is contained in:
@ -157,29 +157,32 @@ namespace units {
|
|||||||
} // namespace detail
|
} // namespace detail
|
||||||
|
|
||||||
template<Quantity To, typename U, typename Rep>
|
template<Quantity To, typename U, typename Rep>
|
||||||
[[nodiscard]] constexpr To quantity_cast(const quantity<U, Rep>& q)
|
[[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>
|
||||||
{
|
{
|
||||||
using c_ratio = ratio_divide<typename U::ratio, typename To::unit::ratio>;
|
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>;
|
using c_rep = std::common_type_t<typename To::rep, Rep, intmax_t>;
|
||||||
using cast = detail::quantity_cast_impl<To, c_ratio, c_rep, c_ratio::num == 1, c_ratio::den == 1>;
|
using ret_dim = downcast<typename To::unit::dimension>;
|
||||||
|
using ret_unit = downcast<unit<ret_dim, typename To::unit::ratio>>;
|
||||||
|
using ret = quantity<ret_unit, typename To::rep>;
|
||||||
|
using cast = detail::quantity_cast_impl<ret, c_ratio, c_rep, c_ratio::num == 1, c_ratio::den == 1>;
|
||||||
return cast::cast(q);
|
return cast::cast(q);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<Unit ToU, QuantityRep ToRep, typename U, typename Rep>
|
template<Unit ToU, QuantityRep ToRep, typename U, typename Rep>
|
||||||
[[nodiscard]] constexpr quantity<ToU, ToRep> quantity_cast(const quantity<U, Rep>& q)
|
[[nodiscard]] constexpr auto quantity_cast(const quantity<U, Rep>& q)
|
||||||
{
|
{
|
||||||
return quantity_cast<quantity<ToU, ToRep>>(q);
|
return quantity_cast<quantity<ToU, ToRep>>(q);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<Unit ToU, typename U, typename Rep>
|
template<Unit ToU, typename U, typename Rep>
|
||||||
[[nodiscard]] constexpr quantity<ToU, Rep> quantity_cast(const quantity<U, Rep>& q)
|
[[nodiscard]] constexpr auto quantity_cast(const quantity<U, Rep>& q)
|
||||||
{
|
{
|
||||||
return quantity_cast<quantity<ToU, Rep>>(q);
|
return quantity_cast<quantity<ToU, Rep>>(q);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<QuantityRep ToRep, typename U, typename Rep>
|
template<QuantityRep ToRep, typename U, typename Rep>
|
||||||
[[nodiscard]] constexpr quantity<U, ToRep> quantity_cast(const quantity<U, Rep>& q)
|
[[nodiscard]] constexpr auto quantity_cast(const quantity<U, Rep>& q)
|
||||||
{
|
{
|
||||||
return quantity_cast<quantity<U, ToRep>>(q);
|
return quantity_cast<quantity<U, ToRep>>(q);
|
||||||
}
|
}
|
||||||
|
@ -269,6 +269,9 @@ namespace {
|
|||||||
|
|
||||||
// quantity_cast
|
// quantity_cast
|
||||||
|
|
||||||
|
static_assert(std::is_same_v<decltype(quantity_cast<unit<length, ratio<1>>>(2km))::unit, metre>);
|
||||||
|
static_assert(std::is_same_v<decltype(quantity_cast<unit<dimension<units::exp<base_dim_length, 1>>, ratio<1>>>(2km))::unit, metre>);
|
||||||
|
|
||||||
// static_assert(quantity_cast<int>(2km).count() == 2000); // should not compile
|
// static_assert(quantity_cast<int>(2km).count() == 2000); // should not compile
|
||||||
static_assert(quantity_cast<quantity<metre, int>>(2km).count() == 2000);
|
static_assert(quantity_cast<quantity<metre, int>>(2km).count() == 2000);
|
||||||
static_assert(quantity_cast<quantity<kilometre, int>>(2000m).count() == 2);
|
static_assert(quantity_cast<quantity<kilometre, int>>(2000m).count() == 2);
|
||||||
|
Reference in New Issue
Block a user