-
That's a little bit of bikeshedding, but nevertheless:
That doesn't work. So:
How would you prefer to compare? All variants look somehow not too elegant... Any other way? |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments 9 replies
-
|
Beta Was this translation helpful? Give feedback.
-
There is no way to ensure you are comparing to value Additionally, with the changes suggested by @burnpanck in #413 (comment), the following will also be correct: if (x == 0 * m) |
Beta Was this translation helpful? Give feedback.
-
@m5k8, please note that in the V2, we now have a dedicated interface to check against zero: https://mpusz.github.io/mp-units/2.0/users_guide/framework_basics/quantity_arithmetics/#comparison-against-zero. |
Beta Was this translation helpful? Give feedback.
-
Great! Now I need to find some free time to migrate my code to mpunits 2.0... |
Beta Was this translation helpful? Give feedback.
-
I actually keep two small utilities in my headers, Here is the code (still v1): namespace detail {
template <bool enable_point, typename CB>
struct SpecialValue {
public:
template <typename CB_>
explicit constexpr SpecialValue(std::integral_constant<bool, enable_point>, CB_ &&cb)
: callback(std::forward<CB_>(cb)) {}
template <std::floating_point F>
explicit(false) constexpr operator F() const {
return F{callback(F{})};
}
// avoid implicitly convertible quantities, as those will cause ambiguous conversions using the float variant above
template <::units::Quantity Q> requires (!std::is_convertible_v<Q,typename Q::rep>)
explicit(false) constexpr operator Q() const {
return Q{callback(typename Q::rep{})};
}
template <::units::QuantityPoint QP>
requires enable_point
explicit(false) constexpr operator QP() const {
return QP{callback(typename QP::rep{})};
}
private:
CB callback;
};
template <bool en, typename CB>
SpecialValue(std::integral_constant<bool, en>, CB &&) -> SpecialValue<en, std::remove_cvref_t<CB>>;
} // namespace detail
static constexpr auto qNaN = detail::SpecialValue{
std::true_type{}, [](auto F) { return std::numeric_limits<decltype(F)>::quiet_NaN(); }};
static constexpr auto qZero =
detail::SpecialValue{std::false_type{}, [](auto F) { return decltype(F){0}; }}; |
Beta Was this translation helpful? Give feedback.
@m5k8, please note that in the V2, we now have a dedicated interface to check against zero: https://mpusz.github.io/mp-units/2.0/users_guide/framework_basics/quantity_arithmetics/#comparison-against-zero.