Deutsch English Français Italiano |
<vf2nin$c52l$3@dont-email.me> 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: David Brown <david.brown@hesbynett.no> Newsgroups: comp.lang.c Subject: Re: constexpr keyword is unnecessary Date: Sun, 20 Oct 2024 12:55:19 +0200 Organization: A noiseless patient Spider Lines: 68 Message-ID: <vf2nin$c52l$3@dont-email.me> References: <veb5fi$3ll7j$1@dont-email.me> <877ca5q84u.fsf@nosuchdomain.example.com> <vf0ijd$3u54q$1@dont-email.me> <vf0l98$3un4n$1@dont-email.me> <8734krrdkh.fsf@nosuchdomain.example.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Sun, 20 Oct 2024 12:55:20 +0200 (CEST) Injection-Info: dont-email.me; posting-host="e075af0a451a2884c4ee8f8ff13dce24"; logging-data="398421"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18LnvegF8Z+YEbIhTOwfE4/mok2uFt/f78=" User-Agent: Mozilla Thunderbird Cancel-Lock: sha1:xU8BUhtLQepNPWHqvpuhGFJaDGA= In-Reply-To: <8734krrdkh.fsf@nosuchdomain.example.com> Content-Language: en-GB Bytes: 3904 On 19/10/2024 23:24, Keith Thompson wrote: > David Brown <david.brown@hesbynett.no> writes: >> 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? Can you think of any >> difference between local variables "a" and "b" defined like this? >> >> enum { n = 2 }; >> const int c = n; >> int a[c]; >> int b[n]; >> >> Can you show any situation where you could use "a" and not "b", or >> vice versa, or where the meaning would be different? Can you show any >> compiler that treats them differently in code generation (assuming a >> compiler that supports enough of C99 to allow it)? >> >> I know of no differences there. That is enough to convince me that it >> doesn't matter in the slightest whether it is technically a VLA or >> not. > > VLAs are optional in C11 and later. A conforming implementation that > doesn't support VLAs will accept `int b[n];` and reject `int a[c];`. Yes. That's why I said "a compiler that supports enough of C99 to allow it". (I think it was a really bad idea to make VLAs optional in C11 - I can't see any justification for it other than pressure from a big compiler vendor that has long neglected implementation of C standards.) > > `sizeof a` is not a constant expression, so it can't be used in a case > label or any other context that requires a constant expression. (If > `const int c = n;` made c a constant expression, this would not apply.) > Ah, there you have a difference. Thanks. > Generated code for constructs that a compiler acccepts is likely to be > identical. For a VLA type with a non-constant length, the compiler must > implicitly store the size somehow. For a VLA type with a length that > the compiler can evaluate at compile time, the compiler is likely to > generate code equivalent to that for a non-VLA. > Yes, that's the point - and that's why I wonder why Thiago is happy with an array with a constant expression size, but dislikes one with a non-constant size that is known and fixed at compile time, just because it is technically a VLA.