1

I am playing around in C++17 with clang version 13.0.0 to test whether a given constexpr value is NaN with wrappers but I keep getting errors with if constexpr but the compiler passes using ternary operator

template <typename T>
constexpr bool is_nan(const T a){
    return (a != a);
};

template <typename T>
constexpr bool checkIfValid(const T a){
    if constexpr(is_nan(a)){
        return true;
    }
    return false;
};

The error shown by clang :

error: constexpr if condition is not a constant expression
        if constexpr(is_value_nan(a)) {
                     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~

If I replace if constexpr with ternary operator then its fine

#This compiles
template <typename T>
constexpr bool checkIfValid(const T a){
   return (is_nan(a) ? true : false);
};
Kish
  • 293
  • 1
  • 6
  • 3
    Parameter `a` is `const` (immutable), but not `constexpr`. You can set the result to `constexpr` only at a higher calling hierarchy level, where it is known, what `a` really is. Making a function `constexpr` only means it can be used with compile-time constant values, but it can also be used during runtime. Why do you want to use `if constexpr` here instead of normal `if`? Normally the data inside `constexpr` functions is not `constexpr` (but it can be). – Sebastian Jan 16 '22 at 17:30

0 Answers0