Deutsch English Français Italiano |
<87y12jpxvl.fsf@nosuchdomain.example.com> View for Bookmarking (what is this?) Look up another Usenet article |
Path: ...!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Keith Thompson <Keith.S.Thompson+u@gmail.com> Newsgroups: comp.lang.c Subject: Re: constexpr keyword is unnecessary Date: Sat, 19 Oct 2024 14:48:46 -0700 Organization: None to speak of Lines: 68 Message-ID: <87y12jpxvl.fsf@nosuchdomain.example.com> References: <veb5fi$3ll7j$1@dont-email.me> <877ca5q84u.fsf@nosuchdomain.example.com> <vf0ijd$3u54q$1@dont-email.me> <vf0l98$3un4n$1@dont-email.me> <vf1216$p0c$1@dont-email.me> MIME-Version: 1.0 Content-Type: text/plain Injection-Date: Sat, 19 Oct 2024 23:48:46 +0200 (CEST) Injection-Info: dont-email.me; posting-host="4632646bb00bcda06c245f19eb191951"; logging-data="46837"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1++gCO2rHm+F35C5GARdHw5" User-Agent: Gnus/5.13 (Gnus v5.13) Cancel-Lock: sha1:utr0/GqqQcxp5qmrKjz2bvs4+kU= sha1:C0uJTPwFvQnGZAE7tWZEPlCcODg= Bytes: 3634 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.com void Void(void) { Void(); } /* The recursive call of the void */