0

In my understanding, if constexpr(consdition) should be evaluated at compile time, at least when condition can always be evaluated at compile time. The same is true for std::enable_if.

However, the following code does not compile:

    template <typename A, typename B>
typename std::enable_if_t<!std::is_same_v<A, B>, size_t> do_something(const A arg) {
    if constexpr (std::is_same_v<A, B>) {
        static_assert(false);
    } else {
        (void) arg;
        return 42;
    }
}

It returns

./test.h:25:13: error: static_assert failed
        static_assert(false);

In my mind, the error means that either enable_if<A != B> or if constexpr(A == B) are not evaluated at compile time.

Why does this not work?

I am using gcc 11.0.1.

Related questions.

TilmannZ
  • 1,643
  • 11
  • 16

0 Answers0