mardi 16 août 2022

c++11 static_assert failed to compare const double variable

See this code snippet:

    const     int i1 = 1;
    constexpr int i2 = 1;
    static_assert(i1 == 1, "const     int is not compile-time const"); // ok
    static_assert(i2 == 1, "constexpr int is not compile-time const"); // ok

    const     double PI1 = 3.0;
    constexpr double PI2 = 3.0;  
    static_assert(PI1 == 3.0, "const     double is not compile-time const");  // error
    static_assert(PI2 == 3.0, "constexpr double is not compile-time const");  // ok

For latest clang-14 and gcc-12, the 3rd static_assert doesn't compile, saying:

read of non-constexpr variable 'PI1' is not allowed in a constant expression

This seems very strange to me: for int type, both const and constexpr compare compiles inside static_assert, but for double type, const cannot compare, but only constexpr passes.

What's the syntax rule behind this error? Thanks.

Aucun commentaire:

Enregistrer un commentaire