mirror of
https://github.com/mpusz/mp-units.git
synced 2025-07-31 10:57:16 +02:00
refactor: a few ratio
functions made constexpr
for clang
This commit is contained in:
10
src/core/include/mp-units/bits/external/hacks.h
vendored
10
src/core/include/mp-units/bits/external/hacks.h
vendored
@ -106,6 +106,16 @@
|
|||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#if UNITS_COMP_CLANG
|
||||||
|
|
||||||
|
#define CONSTEVAL constexpr
|
||||||
|
|
||||||
|
#else
|
||||||
|
|
||||||
|
#define CONSTEVAL consteval
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
#if UNITS_COMP_MSVC
|
#if UNITS_COMP_MSVC
|
||||||
|
|
||||||
#define UNITS_CONSTRAINED_AUTO_WORKAROUND(X)
|
#define UNITS_CONSTRAINED_AUTO_WORKAROUND(X)
|
||||||
|
@ -32,7 +32,7 @@ namespace mp_units {
|
|||||||
namespace detail {
|
namespace detail {
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
[[nodiscard]] consteval T abs(T v) noexcept
|
[[nodiscard]] CONSTEVAL T abs(T v) noexcept
|
||||||
{
|
{
|
||||||
return v < 0 ? -v : v;
|
return v < 0 ? -v : v;
|
||||||
}
|
}
|
||||||
@ -66,7 +66,7 @@ struct ratio {
|
|||||||
std::intmax_t num;
|
std::intmax_t num;
|
||||||
std::intmax_t den;
|
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);
|
gsl_Expects(den != 0);
|
||||||
if (num == 0)
|
if (num == 0)
|
||||||
@ -81,14 +81,14 @@ struct ratio {
|
|||||||
[[nodiscard]] friend consteval bool operator==(ratio, ratio) = default;
|
[[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 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};
|
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)
|
[[nodiscard]] friend consteval ratio operator*(ratio lhs, ratio rhs)
|
||||||
{
|
{
|
||||||
|
Reference in New Issue
Block a user