From 34765a2ab1b698a7bb1360ff9ed72cce71d0a69a Mon Sep 17 00:00:00 2001 From: Mateusz Pusz Date: Thu, 15 Jun 2023 17:31:08 +0300 Subject: [PATCH] refactor: a few `ratio` functions made `constexpr` for clang --- src/core/include/mp-units/bits/external/hacks.h | 10 ++++++++++ src/core/include/mp-units/bits/ratio.h | 10 +++++----- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/src/core/include/mp-units/bits/external/hacks.h b/src/core/include/mp-units/bits/external/hacks.h index 3eabdaed..def77e85 100644 --- a/src/core/include/mp-units/bits/external/hacks.h +++ b/src/core/include/mp-units/bits/external/hacks.h @@ -106,6 +106,16 @@ #endif +#if UNITS_COMP_CLANG + +#define CONSTEVAL constexpr + +#else + +#define CONSTEVAL consteval + +#endif + #if UNITS_COMP_MSVC #define UNITS_CONSTRAINED_AUTO_WORKAROUND(X) diff --git a/src/core/include/mp-units/bits/ratio.h b/src/core/include/mp-units/bits/ratio.h index 2792fa18..89c4a52c 100644 --- a/src/core/include/mp-units/bits/ratio.h +++ b/src/core/include/mp-units/bits/ratio.h @@ -32,7 +32,7 @@ namespace mp_units { namespace detail { template -[[nodiscard]] consteval T abs(T v) noexcept +[[nodiscard]] CONSTEVAL T abs(T v) noexcept { return v < 0 ? -v : v; } @@ -66,7 +66,7 @@ struct ratio { std::intmax_t num; std::intmax_t den; - consteval explicit(false) ratio(std::intmax_t n, std::intmax_t d = 1) : num{n}, den{d} + CONSTEVAL explicit(false) ratio(std::intmax_t n, std::intmax_t d = 1) : num{n}, den{d} { gsl_Expects(den != 0); if (num == 0) @@ -81,14 +81,14 @@ struct ratio { [[nodiscard]] friend consteval bool operator==(ratio, ratio) = default; [[nodiscard]] friend consteval auto operator<=>(ratio lhs, ratio rhs) { return (lhs - rhs).num <=> 0; } - [[nodiscard]] friend consteval ratio operator-(ratio r) { return ratio{-r.num, r.den}; } + [[nodiscard]] friend CONSTEVAL ratio operator-(ratio r) { return ratio{-r.num, r.den}; } - [[nodiscard]] friend consteval ratio operator+(ratio lhs, ratio rhs) + [[nodiscard]] friend CONSTEVAL ratio operator+(ratio lhs, ratio rhs) { return ratio{lhs.num * rhs.den + lhs.den * rhs.num, lhs.den * rhs.den}; } - [[nodiscard]] friend consteval ratio operator-(ratio lhs, ratio rhs) { return lhs + (-rhs); } + [[nodiscard]] friend CONSTEVAL ratio operator-(ratio lhs, ratio rhs) { return lhs + (-rhs); } [[nodiscard]] friend consteval ratio operator*(ratio lhs, ratio rhs) {