Skip to content

Commit

Permalink
Clang support of static_assert(std::abs(...))
Browse files Browse the repository at this point in the history
Clang/LLVM compiler does not support constant expression of `std::abs`.
Implement our own logic.
  • Loading branch information
antonysigma committed Nov 21, 2024
1 parent a16679e commit d2eefc0
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions microsc-psf/inc/units.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,22 @@ operator""_um(long double v) {
return {static_cast<double>(v)};
}

static_assert(std::abs(Meter<double>(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 <typename T>
constexpr T
abs(T x) {
return (x >= 0) ? x : -x;
}

using ::units::Meter;
using ::units::literals::operator""_um;

static_assert(abs(Meter<double>(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

0 comments on commit d2eefc0

Please sign in to comment.