Sujet : Re: constexpr keyword is unnecessary
De : Keith.S.Thompson+u (at) *nospam* gmail.com (Keith Thompson)
Groupes : comp.lang.cDate : 19. Oct 2024, 22:48:46
Autres entêtes
Organisation : None to speak of
Message-ID : <87y12jpxvl.fsf@nosuchdomain.example.com>
References : 1 2 3 4 5
User-Agent : Gnus/5.13 (Gnus v5.13)
Thiago Adams <
thiago.adams@gmail.com> writes:
Em 10/19/2024 1:03 PM, David Brown escreveu:
On 19/10/2024 17:18, Thiago Adams wrote:
Em 10/18/2024 8:54 PM, Keith Thompson escreveu:
Thiago Adams <thiago.adams@gmail.com> writes:
I think constexpr keyword is unnecessary.
>
Sure, most language features are strictly unnecessary.
>
Anything you do with it could/should be done with const.
>
No, absolutely not.
>
>
If not, do you have a sample where, using "const" as "constexpr",
would create problems?
>
The sample I know is VLA.
>
const int c = 2;
int a[c]; //a is VLA because c is not a constant expression.
>
>
But this is not enough to convince me because it is better not to
be a VLA here.
>
What practical difference would it make?
>
I don't see any practical difference. In theory, the generated code
could be different, but I'm arguing that this doesn't really matter
and, consequently, it's not a good reason to differentiate between
const and constexpr.
My reasons for not wanting `const int c = 2;` to make c a constant
expression have nothing to do with any theoretical difference in
generated code.
My reason is that "const" and "constant" are two almost entirely
distinct concepts. Conflating them makes the language more confusing.
Making the name of a "const" object a constant expression adds no new
capabilities beyone what we already have with "constexpr".
Though the C23 standard hasn't yet been officially released, it's too
late to make any substantive changes. C23 *will* have constexpr, and
*will not* treat const-qualified objects as constants.
If I want a name for a constant expression of type int, I can (in C23)
use "constexpr", which clearly expresses that intent. Using "const"
instead, in all versions of C up to and including C23, will result in
compile-time errors.
Let's pretend that when "const" was introduced in C89, it was spelled
"readonly", which more closely reflects its meaning. Would you suggest
that
readonly int n = 42;
should make n a constant expression?
What you propose would make n a constant expression if and only if its
initializer is constant. In C23, n is a constant expression if and only
if n is defined with "constexpr". If you add "constexpr" to a
declaration whose initializer is not a constant expression, it will be
rejected.
-- Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.comvoid Void(void) { Void(); } /* The recursive call of the void */