Home > Back-end >  How can A and not A be both true when using static_assert
How can A and not A be both true when using static_assert

Time:08-04

A very confusing situation involving some constexpr and type traits led me to think the value of an expression is true, when in fact it was both true and false.

https://godbolt.org/z/McYMvxasT


#include <utility>
#include <iostream>

template<typename T>
struct S {

    constexpr int f() const {
        constexpr bool t = std::is_same_v<double, double>;
        static_assert(t);
        static_assert(!t);
        //static_assert(false);

        return 0;
    }

    static const int t = f();
};

int main() {
    //S<int> s;
    //std::cout << S<int>::t;

    return 0;
}

I know that if f() never gets instantiated then static_asserts are skipped but this hypothesis is rejected by uncommenting the line static_assert(false) which does fail. Is this a compiler bug?

CodePudding user response:

It's not both true and false: it's simply not instantiated, so it's not evaluated. If you uncomment the two calls in main, it'll trigger the error.

CodePudding user response:

A template with no well-formed instantiation is ill-formed no diagnostic required. The program is invalid but the compiler is not required to diagnose it.

  • Related