Sujet : Re: constexpr keyword is unnecessary
De : bc (at) *nospam* freeuk.com (Bart)
Groupes : comp.lang.cDate : 12. Oct 2024, 14:53:16
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <vedv0a$5m19$1@dont-email.me>
References : 1
User-Agent : Mozilla Thunderbird
On 11/10/2024 13:25, Thiago Adams wrote:
I think constexpr keyword is unnecessary.
Anything you do with it could/should be done with const.
Even without const , one object like (struct point){.x=1, .y=0} is a constant in my view.
So, for instance, no need for (constexpr struct point){.x=1, .y=0} here.
The VLA could have been the motivation for a new keyword, but I don’t think it matters.
On the other hand, (static struct point){.x=1, .y=0} makes sense.
If constexpr were "no-storage" I think it would make sense but it is not.
It depends on what constexpr means. Does it mean an expression known at compile-time? (Or at load-time when the expression refers to addresses of static memory locations that will not change during the program's run.)
Or does it mean the expression will not change after it's mean assigned?
For example:
const int i = rand();
i = rand(); // not (directly) allowed
Further, if the const/constexr is in a loop, or anywhere in a function where it can be encountered a million times, can the expression be different each time? Example:
while (1) {
const i = rand();
constexpr j = rand(); // if legal
}
C has a problem with named constants, which are variously implemented with #define, enum and const, none of which do the whole job, although enum comes closest.
So where does constexpr fit into all that?
This table refers to examples like '#define A 100', 'eval {B=200}', and 'const int C=300'; Y is the desirable answer in each case:
#define enum const
Scope rules N Y Y
Any type Y N Y
& is an error Y Y N
Value fixed Y Y N (can be changed via casts)
Array bounds Y Y N (statics/module scope)
Array bounds Y Y N to avoid VLAs
Case labels Y Y N
Context free N Y Y (to do with macro expansion)
Fixed eval time Y Y Y
The last one is to do with how long it takes the compiler to evaluate the expression, which is normally negligible.
'const int C=X' depends on how long X takes at runtime.
But 'constexpr int C=X' can depend on how long X takes at compile-time.
What does the column for constexpr look like for the other attributes?