Skip to content

Commit

Permalink
refactor: value_casts cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
mpusz committed Dec 20, 2023
1 parent 436463b commit 7c83f58
Showing 1 changed file with 5 additions and 11 deletions.
16 changes: 5 additions & 11 deletions src/core/include/mp-units/bits/value_cast.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,11 @@ namespace mp_units {
* @tparam ToU a unit to use for a target quantity
*/
template<Unit auto ToU, typename Q>
[[nodiscard]] constexpr Quantity auto value_cast(Q&& q)
requires Quantity<std::remove_cvref_t<Q>> && (convertible(std::remove_reference_t<Q>::reference, ToU))
[[nodiscard]] constexpr Quantity auto value_cast(Q&& q)
{
using q_type = std::remove_reference_t<Q>;
constexpr auto r = [] {
if constexpr (detail::is_specialization_of_reference<std::remove_const_t<decltype(q_type::reference)>>::value ||
!AssociatedUnit<std::remove_const_t<decltype(ToU)>>)
return reference<q_type::quantity_spec, ToU>{};
else
return ToU;
}();
return detail::sudo_cast<quantity<r, typename q_type::rep>>(std::forward<Q>(q));
return detail::sudo_cast<quantity<q_type::quantity_spec[ToU], typename q_type::rep>>(std::forward<Q>(q));
}

/**
Expand Down Expand Up @@ -106,10 +99,11 @@ template<Unit auto ToU, Representation ToRep, typename Q>
* @tparam ToU a unit to use for a target quantity point
*/
template<Unit auto ToU, typename QP>
[[nodiscard]] constexpr QuantityPoint auto value_cast(QP&& qp)
requires QuantityPoint<std::remove_cvref_t<QP>> && (convertible(std::remove_reference_t<QP>::reference, ToU))
[[nodiscard]] constexpr QuantityPoint auto value_cast(QP&& qp)
{
return QP{value_cast<ToU>(std::forward<QP>(qp).quantity_from_origin_is_an_implementation_detail_), qp.point_origin};
return quantity_point{value_cast<ToU>(std::forward<QP>(qp).quantity_from_origin_is_an_implementation_detail_),
qp.point_origin};
}

/**
Expand Down

0 comments on commit 7c83f58

Please sign in to comment.