mirror of
https://github.com/mpusz/mp-units.git
synced 2025-07-30 02:17:16 +02:00
value_cast overloads for ToQ and ToQP
This commit is contained in:
@ -80,7 +80,8 @@ template<Representation ToRep, typename Q>
|
||||
*
|
||||
* auto q = value_cast<us, int>(1.23 * ms);
|
||||
*
|
||||
* @tparam ToRep a representation type to use for a target quantity
|
||||
* @tparam ToU a unit to use for the target quantity
|
||||
* @tparam ToRep a representation type to use for the target quantity
|
||||
*/
|
||||
template<Unit auto ToU, Representation ToRep, typename Q>
|
||||
requires Quantity<std::remove_cvref_t<Q>> && (convertible(std::remove_reference_t<Q>::reference, ToU)) &&
|
||||
@ -92,6 +93,30 @@ template<Unit auto ToU, Representation ToRep, typename Q>
|
||||
return detail::sudo_cast<quantity<detail::make_reference(q_type::quantity_spec, ToU), ToRep>>(std::forward<Q>(q));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @brief Explicit cast of a quantity's representation
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* using ToQ = quantity<us, int>;
|
||||
* auto q = value_cast<ToQ>(1.23 * ms);
|
||||
*
|
||||
* Note that value_cast only changes the "representation aspects" (unit and representation
|
||||
* type), but not the "meaning" (quantity type).
|
||||
*
|
||||
* @tparam ToQ a target quantity type to which to cast the representation
|
||||
*/
|
||||
template<Quantity ToQ, typename Q>
|
||||
requires Quantity<std::remove_cvref_t<Q>> && (convertible(std::remove_reference_t<Q>::reference, ToQ::unit)) &&
|
||||
(ToQ::quantity_spec == std::remove_reference_t<Q>::quantity_spec) &&
|
||||
std::constructible_from<typename ToQ::rep, typename std::remove_reference_t<Q>::rep>
|
||||
[[nodiscard]] constexpr Quantity auto value_cast(Q&& q)
|
||||
{
|
||||
return detail::sudo_cast<ToQ>(std::forward<Q>(q));
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Explicit cast of a quantity point's unit
|
||||
*
|
||||
@ -133,14 +158,15 @@ value_cast(QP&& qp)
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Explicit cast of a quantity's unit and representation type
|
||||
* @brief Explicit cast of a quantity point's unit and representation type
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* auto q = value_cast<us, int>(1.23 * ms);
|
||||
* auto qp = value_cast<us, int>(quantity_point{1.23 * ms});
|
||||
*
|
||||
* @tparam ToRep a representation type to use for a target quantity
|
||||
* @tparam ToU a unit to use for the target quantity
|
||||
* @tparam ToRep a representation type to use for the target quantity
|
||||
*/
|
||||
template<Unit auto ToU, Representation ToRep, typename QP>
|
||||
requires QuantityPoint<std::remove_cvref_t<QP>> && (convertible(std::remove_reference_t<QP>::reference, ToU)) &&
|
||||
@ -152,4 +178,64 @@ template<Unit auto ToU, Representation ToRep, typename QP>
|
||||
std::remove_reference_t<QP>::point_origin};
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Explicit cast of a quantity point's representation
|
||||
*
|
||||
* 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 and representation
|
||||
* type), but not the "meaning" (quantity type or the actual point that is being described).
|
||||
*
|
||||
* @tparam ToQ a target quantity type to which to cast the representation of the point
|
||||
*/
|
||||
template<Quantity ToQ, typename QP>
|
||||
requires QuantityPoint<std::remove_cvref_t<QP>> && (convertible(std::remove_reference_t<QP>::reference, ToQ::unit)) &&
|
||||
(ToQ::quantity_spec == std::remove_reference_t<QP>::quantity_spec) &&
|
||||
std::constructible_from<typename ToQ::rep, typename std::remove_reference_t<QP>::rep>
|
||||
[[nodiscard]] constexpr QuantityPoint auto value_cast(QP&& qp)
|
||||
{
|
||||
return quantity_point{value_cast<ToQ>(std::forward<QP>(qp).quantity_from_origin_is_an_implementation_detail_),
|
||||
std::remove_reference_t<QP>::point_origin};
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Explicit cast of a quantity point's representation, including potentially the point origin
|
||||
*
|
||||
* 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).
|
||||
*
|
||||
* @tparam ToQP a target quantity point type to which to cast the representation of the point
|
||||
*/
|
||||
template<QuantityPoint ToQP, typename QP>
|
||||
requires QuantityPoint<std::remove_cvref_t<QP>> &&
|
||||
(convertible(std::remove_reference_t<QP>::reference, ToQP::unit)) &&
|
||||
(ToQP::quantity_spec == std::remove_reference_t<QP>::quantity_spec) &&
|
||||
(detail::same_absolute_point_origins(ToQP::point_origin, std::remove_reference_t<QP>::point_origin)) &&
|
||||
std::constructible_from<typename ToQP::rep, typename std::remove_reference_t<QP>::rep>
|
||||
[[nodiscard]] constexpr QuantityPoint auto value_cast(QP&& qp)
|
||||
{
|
||||
return quantity_point{
|
||||
value_cast<typename ToQP::quantity_type>(std::forward<QP>(qp).quantity_from_origin_is_an_implementation_detail_),
|
||||
std::remove_reference_t<QP>::point_origin}
|
||||
.point_for(ToQP::point_origin);
|
||||
}
|
||||
|
||||
|
||||
} // namespace mp_units
|
||||
|
@ -1692,4 +1692,25 @@ static_assert(value_cast<float>(lvalue_qp).quantity_from_zero().numerical_value_
|
||||
static_assert(value_cast<m, float>(lvalue_qp).quantity_from_zero().numerical_value_in(m) == 2000.f);
|
||||
} // 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_point<km>>(quantity_point{2000 * m}).quantity_from_zero().numerical_value_in(km) ==
|
||||
2);
|
||||
static_assert(
|
||||
!requires(quantity_point<isq::width[m]> qp) { value_cast<quantity<m>>(qp); },
|
||||
"value_cast shall not cast between different quantity types");
|
||||
static_assert(
|
||||
!requires(quantity_point<m> qp) { value_cast<quantity<isq::width[m]>>(qp); },
|
||||
"value_cast shall not cast between different quantity types");
|
||||
static_assert(value_cast<quantity_point<m, mean_sea_level>>(quantity_point<km, ground_level>{2 * km})
|
||||
.quantity_ref_from(mean_sea_level)
|
||||
.numerical_value_in(m) == 2042);
|
||||
static_assert(value_cast<quantity_point<cm, mean_sea_level, int>>(quantity_point<mm, ground_level, std::int8_t>{
|
||||
std::int8_t{100} * mm})
|
||||
.quantity_ref_from(mean_sea_level)
|
||||
.numerical_value_in(cm) == 4210);
|
||||
static_assert(value_cast<quantity_point<mm, ground_level, std::int8_t>>(quantity_point<cm, mean_sea_level>{4210 * cm})
|
||||
.quantity_ref_from(ground_level)
|
||||
.numerical_value_in(mm) == 100);
|
||||
|
||||
|
||||
} // namespace
|
||||
|
Reference in New Issue
Block a user