From defc69b346c8a6366876d790e13005887863f8cd Mon Sep 17 00:00:00 2001 From: Mateusz Pusz Date: Wed, 2 Oct 2024 19:05:45 +0200 Subject: [PATCH] refactor: :boom: `mag_constant` now takes a symbol and a value and the class deriving from it must be final --- .../framework_basics/systems_of_units.md | 4 +-- .../include/mp-units/framework/magnitude.h | 28 +++++++++++++++---- 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/docs/users_guide/framework_basics/systems_of_units.md b/docs/users_guide/framework_basics/systems_of_units.md index 6f61cc10..57d4d592 100644 --- a/docs/users_guide/framework_basics/systems_of_units.md +++ b/docs/users_guide/framework_basics/systems_of_units.md @@ -184,9 +184,7 @@ For some units, a magnitude might also be irrational. The best example here is a is defined using a floating-point magnitude having a factor of the number π (Pi): ```cpp -inline constexpr struct pi : mag_constant { - static constexpr auto value = std::numbers::pi_v; -} pi; +inline constexpr struct pi final : mag_constant> {} pi; ``` ```cpp diff --git a/src/core/include/mp-units/framework/magnitude.h b/src/core/include/mp-units/framework/magnitude.h index 7159effc..3ec0254c 100644 --- a/src/core/include/mp-units/framework/magnitude.h +++ b/src/core/include/mp-units/framework/magnitude.h @@ -56,13 +56,27 @@ using factorizer = wheel_factorizer<4>; } // namespace detail -MP_UNITS_EXPORT struct mag_constant {}; +#if defined MP_UNITS_COMP_CLANG || MP_UNITS_COMP_CLANG < 18 -template -concept MagConstant = std::derived_from && std::is_empty_v && requires { - { +T::value } -> std::same_as; +MP_UNITS_EXPORT template +struct mag_constant {}; + +#else + +MP_UNITS_EXPORT template +struct mag_constant { + static constexpr auto value = Value; }; +#endif + +MP_UNITS_EXPORT template +concept MagConstant = + is_derived_from_specialization_of_v && std::is_empty_v && std::is_final_v && requires { + { +T::value } -> std::same_as; + }; + + /** * @brief Any type which can be used as a basis vector in a PowerV. * @@ -598,8 +612,12 @@ constexpr Magnitude auto mag_power = pow(mag); /** * @brief A convenient Magnitude constant for pi, which we can manipulate like a regular number. */ -inline constexpr struct pi : mag_constant { +#if defined MP_UNITS_COMP_CLANG || MP_UNITS_COMP_CLANG < 18 +inline constexpr struct pi final : mag_constant { static constexpr auto value = std::numbers::pi_v; +#else +inline constexpr struct pi final : mag_constant> { +#endif } pi; [[deprecated("Use `mag` instead")]] inline constexpr Magnitude auto mag_pi = mag;