<mp-units/framework/value_cast.h>
template<
QuantityPoint ToQP,
typename FwdQP,
QuantityPoint QP = std::remove_cvref_t<FwdQP>>
requires (ToQP::quantity_spec == QP::quantity_spec) &&
(MP_UNITS_WEAK_UNIT_OF(MP_UNITS_NONCONST_TYPE(ToQP::unit), QP::quantity_spec)) &&
(detail::same_absolute_point_origins(ToQP::point_origin, QP::point_origin)) &&
std::constructible_from<typename ToQP::rep, typename QP::rep> &&
detail::SaneScaling<QP::unit, ToQP::unit, typename ToQP::rep>
[[nodiscard]]
constexpr
QuantityPoint auto
value_cast(FwdQP&& qp);
Implicit conversions between quantities of different types are allowed only for "safe" (e.g. non-truncating) conversion. In truncating cases an explicit cast have to be used.
inline constexpr struct A : absolute_point_origin<A, isq::distance> A; inline constexpr struct B : relative_point_origin<A + 1*m> B;
using ToQP = quantity_point<mm, B, int>; auto qp = value_cast<ToQP>(quantity_point{1.23 m});
Note that value_cast only changes the "representation aspects" (unit, representation type and point origin), but not the "meaning" (quantity type or the actual point that is being described).
Note also that changing the point origin bears risks regarding truncation and overflow similar to other casts that change representation (which is why we require a value_cast
and disallow implicit conversions). This cast is guaranteed not to cause overflow of any intermediate representation type provided that the input quantity point is within the range of ToQP
. Calling value_cast<ToQP>(qp)
on a qp
outside of the range of ToQP
is potentially undefined behaviour. The implementation further attempts not to cause more than rounding error than approximately the sum of the resolution of qp
as represented in FromQP
, plust the resolution of qp
as represented in ToQP
.
Name | Description |
---|---|
ToQP | a target quantity point type to which to cast the representation of the point |