Sujet : Re: constexpr keyword is unnecessary
De : thiago.adams (at) *nospam* gmail.com (Thiago Adams)
Groupes : comp.lang.cDate : 12. Oct 2024, 22:43:14
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <veeqhi$ar0c$2@dont-email.me>
References : 1 2
User-Agent : Mozilla Thunderbird
Em 10/12/2024 10:53 AM, Bart escreveu:
It depends on what constexpr means.
It means the initialization expression must be evaluated at compilation.
Also the declarator can be used in another expression as constant.
Sample
constexpr int a = 1;
constexpr int b = a+1;
But the expression is always something the compiler need to check if is constant or not.
So, what I suggest is if the init expression is a constant expression (something we know at compile time) then the declarator is real constexpr (no need for a new keyword)
For instance:
const int a = 1;
const int b = a+1;
Even if not constant, the compiler can do at compile time.
for instance.
int a = 1+2;
So this has to be done anyway.
The only difference is that using 'a' cannot be used as constant in another expression.