From 9d48e6983a2ba62b5666ffd203a6581bdf25c82c Mon Sep 17 00:00:00 2001 From: Chip Hogg Date: Sat, 8 Jan 2022 14:40:28 -0500 Subject: [PATCH] Refactor BasePower struct and concept Instead of templating on `long double`, we template on "anything whose value member is `long double`". This is how we work around gcc-10's lack of support for floating point NTTPs. To access the base value, we provide `.get_base()` member functions. The clearest and most direct way I know to express "is this a base power" is via a type trait, which is true only for `base_power`. --- src/core/include/units/magnitude.h | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/src/core/include/units/magnitude.h b/src/core/include/units/magnitude.h index 09343c86..4e35a4fb 100644 --- a/src/core/include/units/magnitude.h +++ b/src/core/include/units/magnitude.h @@ -44,7 +44,7 @@ constexpr bool is_prime(std::intmax_t n); // Integer rep is for prime numbers; long double is for any irrational base we permit. template -concept BaseRep = std::is_same_v || std::is_same_v; +concept BaseRep = std::is_same_v || std::is_same_v; /** * @brief A basis vector in our magnitude representation, raised to some rational power. @@ -60,11 +60,21 @@ concept BaseRep = std::is_same_v || std::is_same_v; */ template struct base_power { - // The value of the basis vector. - T base; + // The rational power to which the base is raised. + ratio power{1}; + + constexpr long double get_base() const { return T::value; } +}; + +template<> +struct base_power { + // The value of the basis "vector". + int base; // The rational power to which the base is raised. ratio power{1}; + + constexpr int get_base() const { return base; } }; template U> @@ -94,10 +104,15 @@ constexpr bool is_valid_base_power(const base_power &bp) { else if constexpr (std::is_same_v) { return bp.base > 0; } else { return false; } // Unreachable. } + +template +struct is_base_power : std::false_type {}; +template +struct is_base_power> : std::true_type {}; } // namespace detail template -concept BasePower = std::is_same_v> || std::is_same_v>; +concept BasePower = detail::is_base_power::value; /** * @brief A representation for positive real numbers which optimizes taking products and rational powers. @@ -107,7 +122,7 @@ concept BasePower = std::is_same_v> || std::is_same_v requires requires { - // (is_valid_base_power(BasePowers) && ... && strictly_increasing(std::make_tuple(BasePowers.base...))); + // (is_valid_base_power(BasePowers) && ... && strictly_increasing(BasePowers.base...)); (detail::is_valid_base_power(BasePowers) && ...); } struct magnitude {};