mirror of
https://github.com/mpusz/mp-units.git
synced 2025-07-30 18:37:15 +02:00
refactor: integral conversion factor optimized for the case of the same unit type
This commit is contained in:
@ -45,17 +45,22 @@ namespace mp_units {
|
|||||||
|
|
||||||
namespace detail {
|
namespace detail {
|
||||||
|
|
||||||
template<auto UFrom, auto UTo>
|
template<Unit UFrom, Unit UTo>
|
||||||
concept IntegralConversionFactor = Unit<decltype(UFrom)> && Unit<decltype(UTo)> &&
|
[[nodiscard]] consteval bool integral_conversion_factor(UFrom from, UTo to)
|
||||||
decltype(is_integral(decltype(decltype(get_canonical_unit(UFrom))::mag /
|
{
|
||||||
decltype(get_canonical_unit(UTo))::mag){}))::value;
|
if constexpr (is_same_v<UFrom, UTo>)
|
||||||
|
return true;
|
||||||
|
else
|
||||||
|
return decltype(is_integral(
|
||||||
|
decltype(decltype(get_canonical_unit(from))::mag / decltype(get_canonical_unit(to))::mag){}))::value;
|
||||||
|
}
|
||||||
|
|
||||||
template<typename QFrom, typename QTo>
|
template<typename QFrom, typename QTo>
|
||||||
concept QuantityConvertibleTo =
|
concept QuantityConvertibleTo =
|
||||||
Quantity<QFrom> && Quantity<QTo> && implicitly_convertible(QFrom::quantity_spec, QTo::quantity_spec) &&
|
Quantity<QFrom> && Quantity<QTo> && implicitly_convertible(QFrom::quantity_spec, QTo::quantity_spec) &&
|
||||||
convertible(QFrom::unit, QTo::unit) &&
|
convertible(QFrom::unit, QTo::unit) &&
|
||||||
(treat_as_floating_point<typename QTo::rep> ||
|
(treat_as_floating_point<typename QTo::rep> ||
|
||||||
(!treat_as_floating_point<typename QFrom::rep> && IntegralConversionFactor<QFrom::unit, QTo::unit>)) &&
|
(!treat_as_floating_point<typename QFrom::rep> && (integral_conversion_factor(QFrom::unit, QTo::unit)))) &&
|
||||||
// TODO consider providing constraints of sudo_cast here rather than testing if it can be called (its return type is
|
// TODO consider providing constraints of sudo_cast here rather than testing if it can be called (its return type is
|
||||||
// deduced thus the function is evaluated here and may emit truncating conversion or other warnings)
|
// deduced thus the function is evaluated here and may emit truncating conversion or other warnings)
|
||||||
requires(QFrom q) { detail::sudo_cast<QTo>(q); };
|
requires(QFrom q) { detail::sudo_cast<QTo>(q); };
|
||||||
|
Reference in New Issue
Block a user