From d2eefc00b09a2356a1ad028daf2f6649bf7d21ea Mon Sep 17 00:00:00 2001 From: Antony Chan Date: Thu, 21 Nov 2024 12:02:08 -0800 Subject: [PATCH] Clang support of `static_assert(std::abs(...))` Clang/LLVM compiler does not support constant expression of `std::abs`. Implement our own logic. --- microsc-psf/inc/units.h | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/microsc-psf/inc/units.h b/microsc-psf/inc/units.h index cf1e864..cc61d2c 100644 --- a/microsc-psf/inc/units.h +++ b/microsc-psf/inc/units.h @@ -48,8 +48,22 @@ operator""_um(long double v) { return {static_cast(v)}; } -static_assert(std::abs(Meter(1.5_um) - 1.5e-6) < 1e-6, "Micrometer value out of scale"); -static_assert(std::abs((1.5_m).value - 1.5) < 1e-6, "Micrometer value out of scale"); +namespace internal { + +/** LLVM's abs is not a constant expression. Make our own function */ +template +constexpr T +abs(T x) { + return (x >= 0) ? x : -x; +} + +using ::units::Meter; +using ::units::literals::operator""_um; + +static_assert(abs(Meter(1.5_um) - 1.5e-6) < 1e-6, "Micrometer value out of scale"); +static_assert(abs((1.5_m).value - 1.5) < 1e-6, "Micrometer value out of scale"); + +} // namespace internal } // namespace literals } // namespace units