mercredi 26 août 2015

g++ accepts const as constexpr in static_assert

When compiling the following code with g++ 4.8.2 and -std=c++11 flag it compiles with no errors:

    constexpr double C = 299792.458;
    const double local_max = 3.5;
    static_assert(local_max < C, "can't go that fast");

When compiling the following code it gives error:

    constexpr double C = 299792.458;
    double x = 3.5;
    const double local_max = x;
    static_assert(local_max < C, "can't go that fast");

Error message:

a.cc: In function ‘int main()’:

a.cc:6:2: error: non-constant condition for static assertion
static_assert(local_max < C, "can't go that fast");

a.cc:6:2: error: the value of ‘local_max’ is not usable in a constant expression

a.cc:5:15: note: ‘local_max’ was not declared ‘constexpr’ const double local_max = x;

My question is why it does not give error in the first case.

Does it depend on whether a const variable is initialized with a constexpr or not?

Aucun commentaire:

Enregistrer un commentaire