2 years ago

#57814

test-img

Kish

Ternary operator works with constexpr expressions but `if constexpr` does

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);
};

c++

c++17

constexpr

clang++

if-constexpr

0 Answers

Your Answer

Accepted video resources