mirror of
https://github.com/mpusz/mp-units.git
synced 2025-07-30 10:27:16 +02:00
feat: value_cast<Representation, Unit>()
complementary conversion function added
This commit is contained in:
@ -182,11 +182,11 @@ which may well be outside the range of one or both quantity types.
|
|||||||
The table below provides all the value conversions functions that may be run on `x` being the
|
The table below provides all the value conversions functions that may be run on `x` being the
|
||||||
instance of either `quantity` or `quantity_point`:
|
instance of either `quantity` or `quantity_point`:
|
||||||
|
|
||||||
| Forcing | Representation | Unit | Member function | Conversion function |
|
| Forcing | Representation | Unit | Member function | Non-member function |
|
||||||
|:-------:|:--------------:|:----:|--------------------|-----------------------|
|
|:-------:|:--------------:|:----:|--------------------|------------------------------------------------|
|
||||||
| No | Same | `u` | `x.in(u)` | |
|
| No | Same | `u` | `x.in(u)` | |
|
||||||
| No | `T` | Same | `x.in<T>()` | |
|
| No | `T` | Same | `x.in<T>()` | |
|
||||||
| No | `T` | `u` | `x.in<T>(u)` | |
|
| No | `T` | `u` | `x.in<T>(u)` | |
|
||||||
| Yes | Same | `u` | `x.force_in(u)` | `value_cast<u>(x)` |
|
| Yes | Same | `u` | `x.force_in(u)` | `value_cast<u>(x)` |
|
||||||
| Yes | `T` | Same | `x.force_in<T>()` | `value_cast<T>(x)` |
|
| Yes | `T` | Same | `x.force_in<T>()` | `value_cast<T>(x)` |
|
||||||
| Yes | `T` | `u` | `x.force_in<T>(u)` | `value_cast<u, T>(x)` |
|
| Yes | `T` | `u` | `x.force_in<T>(u)` | `value_cast<u, T>(x)` or `value_cast<T, u>(x)` |
|
||||||
|
@ -88,6 +88,14 @@ template<Unit auto ToU, Representation ToRep, typename FwdQ, Quantity Q = std::r
|
|||||||
return detail::sudo_cast<quantity<detail::make_reference(Q::quantity_spec, ToU), ToRep>>(std::forward<FwdQ>(q));
|
return detail::sudo_cast<quantity<detail::make_reference(Q::quantity_spec, ToU), ToRep>>(std::forward<FwdQ>(q));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template<Representation ToRep, Unit auto ToU, typename FwdQ, Quantity Q = std::remove_cvref_t<FwdQ>>
|
||||||
|
requires(convertible(Q::reference, ToU)) && RepresentationOf<ToRep, Q::quantity_spec.character> &&
|
||||||
|
std::constructible_from<ToRep, typename Q::rep>
|
||||||
|
[[nodiscard]] constexpr Quantity auto value_cast(FwdQ&& q)
|
||||||
|
{
|
||||||
|
return detail::sudo_cast<quantity<detail::make_reference(Q::quantity_spec, ToU), ToRep>>(std::forward<FwdQ>(q));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Explicit cast of a quantity's representation
|
* @brief Explicit cast of a quantity's representation
|
||||||
@ -168,6 +176,16 @@ template<Unit auto ToU, Representation ToRep, typename FwdQP, QuantityPoint QP =
|
|||||||
QP::point_origin};
|
QP::point_origin};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template<Representation ToRep, Unit auto ToU, typename FwdQP, QuantityPoint QP = std::remove_cvref_t<FwdQP>>
|
||||||
|
requires(convertible(QP::reference, ToU)) && RepresentationOf<ToRep, QP::quantity_spec.character> &&
|
||||||
|
std::constructible_from<ToRep, typename QP::rep>
|
||||||
|
[[nodiscard]] constexpr QuantityPoint auto value_cast(FwdQP&& qp)
|
||||||
|
{
|
||||||
|
return quantity_point{
|
||||||
|
value_cast<ToU, ToRep>(std::forward<FwdQP>(qp).quantity_from_origin_is_an_implementation_detail_),
|
||||||
|
QP::point_origin};
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Explicit cast of a quantity point's representation
|
* @brief Explicit cast of a quantity point's representation
|
||||||
*
|
*
|
||||||
|
@ -1725,6 +1725,7 @@ constexpr quantity_point lvalue_qp{2 * km};
|
|||||||
static_assert(value_cast<m>(lvalue_qp).quantity_from_zero().numerical_value_in(m) == 2000);
|
static_assert(value_cast<m>(lvalue_qp).quantity_from_zero().numerical_value_in(m) == 2000);
|
||||||
static_assert(value_cast<float>(lvalue_qp).quantity_from_zero().numerical_value_in(km) == 2.f);
|
static_assert(value_cast<float>(lvalue_qp).quantity_from_zero().numerical_value_in(km) == 2.f);
|
||||||
static_assert(value_cast<m, float>(lvalue_qp).quantity_from_zero().numerical_value_in(m) == 2000.f);
|
static_assert(value_cast<m, float>(lvalue_qp).quantity_from_zero().numerical_value_in(m) == 2000.f);
|
||||||
|
static_assert(value_cast<float, m>(lvalue_qp).quantity_from_zero().numerical_value_in(m) == 2000.f);
|
||||||
} // namespace lvalue_tests
|
} // namespace lvalue_tests
|
||||||
|
|
||||||
static_assert(value_cast<quantity<km, int>>(quantity_point{2000 * m}).quantity_from_zero().numerical_value_in(km) == 2);
|
static_assert(value_cast<quantity<km, int>>(quantity_point{2000 * m}).quantity_from_zero().numerical_value_in(km) == 2);
|
||||||
|
@ -1014,6 +1014,7 @@ static_assert(value_cast<km / h>(2000.0 * m / (3600.0 * s)).numerical_value_in(k
|
|||||||
|
|
||||||
static_assert(value_cast<int>(1.23 * m).numerical_value_in(m) == 1);
|
static_assert(value_cast<int>(1.23 * m).numerical_value_in(m) == 1);
|
||||||
static_assert(value_cast<km, int>(1.23 * m).numerical_value_in(km) == 0);
|
static_assert(value_cast<km, int>(1.23 * m).numerical_value_in(km) == 0);
|
||||||
|
static_assert(value_cast<int, km>(1.23 * m).numerical_value_in(km) == 0);
|
||||||
|
|
||||||
static_assert((2 * km).force_in(m).numerical_value_in(m) == 2000);
|
static_assert((2 * km).force_in(m).numerical_value_in(m) == 2000);
|
||||||
static_assert((2000 * m).force_in(km).numerical_value_in(km) == 2);
|
static_assert((2000 * m).force_in(km).numerical_value_in(km) == 2);
|
||||||
|
Reference in New Issue
Block a user