jeudi 15 juillet 2021

How do we understand "if possible" about constexpr

I've read the link about constexpr: https://en.cppreference.com/w/cpp/language/constexpr

It says that the constexpr specifier declares that it is possible to evaluate the value of the function or variable at compile time.

As my understanding, it means that a variable specified by constexpr may or may not be evaluated at compile time.

This link told me the same thing: constexpr variables are not compile-time values.

However, I made a test, which confused me: https://godbolt.org/z/9ajb4xKK8

As you see, the variable a must be evaluated at compile time, otherwise it can't be used to declare an array. And the variable s can't be specified by constexpr because the constructor of std::string can't be invoked at compile time.

In a word, it seems that this testing is telling me that constexpr MUST evaluate the value at compile time. Otherwise, how can the variable a be used to declare an array?

What did I wrongly understand?

Aucun commentaire:

Enregistrer un commentaire