feat: expr_pow extended to remove redundancy in callers

This commit is contained in:
Mateusz Pusz
2024-10-20 09:46:22 +02:00
parent a7fcb1d40f
commit efb9711f94
4 changed files with 17 additions and 33 deletions

View File

@@ -204,14 +204,8 @@ template<std::intmax_t Num, std::intmax_t Den = 1, Dimension D>
requires detail::non_zero<Den>
[[nodiscard]] consteval Dimension auto pow(D d)
{
if constexpr (detail::BaseDimension<D>) {
if constexpr (Den == 1)
return derived_dimension<power<D, Num>>{};
else
return derived_dimension<power<D, Num, Den>>{};
} else
return detail::expr_pow<Num, Den, derived_dimension, struct dimension_one,
detail::type_list_of_base_dimension_less>(d);
return detail::expr_pow<Num, Den, derived_dimension, struct dimension_one, detail::type_list_of_base_dimension_less>(
d);
}
/**

View File

@@ -475,9 +475,18 @@ template<std::intmax_t Num, std::intmax_t Den, template<typename...> typename To
template<std::intmax_t Num, std::intmax_t Den, template<typename...> typename To, typename OneType,
template<typename, typename> typename Pred, typename T>
requires detail::non_zero<Den>
[[nodiscard]] consteval auto expr_pow(T)
[[nodiscard]] consteval auto expr_pow(T v)
{
if constexpr (Num == 0 || is_same_v<T, OneType>)
return OneType{};
else if constexpr (detail::ratio{Num, Den} == 1)
return v;
else if constexpr (is_specialization_of<T, To>)
return expr_pow_impl<Num, Den, To, OneType, Pred>(typename T::_num_{}, typename T::_den_{});
else if constexpr (Den == 1)
return To<power<T, Num>>{};
else
return To<power<T, Num, Den>>{};
}

View File

@@ -590,18 +590,9 @@ template<std::intmax_t Num, std::intmax_t Den = 1, QuantitySpec Q>
requires detail::non_zero<Den>
[[nodiscard]] consteval QuantitySpec auto pow(Q q)
{
if constexpr (Num == 0 || Q{} == dimensionless)
return dimensionless;
else if constexpr (detail::ratio{Num, Den} == 1)
return q;
else if constexpr (detail::DerivedQuantitySpec<Q>)
return detail::clone_kind_of<Q{}>(
detail::expr_pow<Num, Den, derived_quantity_spec, struct dimensionless, detail::type_list_of_quantity_spec_less>(
detail::remove_kind(q)));
else if constexpr (Den == 1)
return detail::clone_kind_of<Q{}>(derived_quantity_spec<power<decltype(detail::remove_kind(Q{})), Num>>{});
else
return detail::clone_kind_of<Q{}>(derived_quantity_spec<power<decltype(detail::remove_kind(Q{})), Num, Den>>{});
}

View File

@@ -26,7 +26,6 @@
#include <mp-units/bits/get_associated_quantity.h>
#include <mp-units/bits/hacks.h>
#include <mp-units/bits/module_macros.h>
#include <mp-units/bits/ratio.h>
#include <mp-units/bits/text_tools.h>
#include <mp-units/compat_macros.h>
#include <mp-units/ext/algorithm.h>
@@ -608,16 +607,7 @@ template<std::intmax_t Num, std::intmax_t Den = 1, Unit U>
requires detail::non_zero<Den>
[[nodiscard]] consteval Unit auto pow(U u)
{
if constexpr (Num == 0 || is_same_v<U, struct one>)
return one;
else if constexpr (detail::ratio{Num, Den} == 1)
return u;
else if constexpr (is_specialization_of<U, derived_unit>)
return detail::expr_pow<Num, Den, derived_unit, struct one, detail::type_list_of_unit_less>(u);
else if constexpr (Den == 1)
return derived_unit<power<U, Num>>{};
else
return derived_unit<power<U, Num, Den>>{};
}
/**