Deutsch English Français Italiano |
<vf2mno$c52l$1@dont-email.me> View for Bookmarking (what is this?) Look up another Usenet article |
Path: ...!news.nobody.at!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:40:56 +0200 Organization: A noiseless patient Spider Lines: 78 Message-ID: <vf2mno$c52l$1@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> <vf0ps2$3vf16$1@dont-email.me> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Injection-Date: Sun, 20 Oct 2024 12:40:56 +0200 (CEST) Injection-Info: dont-email.me; posting-host="e075af0a451a2884c4ee8f8ff13dce24"; logging-data="398421"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18w1L+NV1Bp5N4rPKwh3f5ovSwlRVx+Rnw=" User-Agent: Mozilla Thunderbird Cancel-Lock: sha1:fVKT+3b/+0WuH/u21C68gIHuzNo= Content-Language: en-GB In-Reply-To: <vf0ps2$3vf16$1@dont-email.me> Bytes: 4234 On 19/10/2024 19:22, Bart wrote: > On 19/10/2024 17:03, David Brown wrote: >> 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. > > I can give examples where a compiler won't work, or it will generate > different code, but you won't execpt it because you won't recognise the > compiler, or will dismiss it, or will dismiss even gcc because > optimisations aren't used. I know there are compilers that don't support VLAs at all - that's fair enough (and that's why I specifically mentioned it). We can expect that compilers that can't handle VLAs at all will not support C23 constexpr, or any suggested C++ style extensions to the semantics of "const". If you have an example of a compiler that /does/ support VLAs, but generates different code for "a" and "b" above, then it would be interesting to hear about it. I will put a lot more weight on its relevance if it is a mainstream or established compiler (it is a big advantage if it is on godbolt.org), and one that does at least some level of optimisation. (For compilers that have no optimisation, or where optimisation is disabled, code quality really doesn't matter anyway.) Single user compilers, on the other hand, have little relevance beyond that one user, and therefore do not matter much. > > If it will always be gcc-O2 or higher, and it is an example just like > these (so inside a function), then probably it will generate the same code. > > But that's a lot of 'if's. > > Where the code is different, then gcc on Windows for example likes to > generate a call to __chkstack() (to incrementally increase the stack a > page at a time in case it skips a page for a large allocation). > > Not with -O1.