Path: csiph.com!news.swapon.de!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Keith Thompson Newsgroups: comp.lang.c Subject: Re: C23 thoughts and opinions Date: Thu, 23 May 2024 14:49:24 -0700 Organization: None to speak of Lines: 51 Message-ID: <87ed9s42sb.fsf@nosuchdomain.example.com> References: <00297443-2fee-48d4-81a0-9ff6ae6481e4@gmail.com> <9be0c55c-38f8-496c-b335-84ad281e1753@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit Injection-Date: Thu, 23 May 2024 23:49:25 +0200 (CEST) Injection-Info: dont-email.me; posting-host="04393c0e0d87ab29eec6bf33d59d4ed3"; logging-data="2047245"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/dQoGHn2ygjuyJZSNh1M/R" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux) Cancel-Lock: sha1:NwES2aioZe8eLKtjNpPRqsl+j8E= sha1:Fp9NUUcab9cJ+Q1omXSHeJAOzb4= Xref: csiph.com comp.lang.c:384923 Thiago Adams writes: > On 23/05/2024 10:11, David Brown wrote: >> On 23/05/2024 14:38, Thiago Adams wrote: [...] >>> I already see bad usages of constexpr in C++ code. It was used in >>> cases where we know for sure that is NOT compile time. This just >>> make review harder "why did someone put this here?" conclusion was >>> it was totally unnecessary and ignored by the compiler. The >>> programmer was trying to add something extra, like "magic" hoping >>> for something that would never happen. >>> >> IME poor or confusing uses of "constexpr" are for functions, not >> objects, and C23 does not support "constexpr" for functions. > > The sample C++ was something like > > constexpr char * s[] = {"a", "b"}; > for (int i = 0; i < sizeof(s); i++) > { > //using s[i] > } > > I checked in C, it is an error. Apparently C23 has stricter rules for constexpr than C++ does. I can imagine those rules being relaxed in future editions of the C standard. > error: 'constexpr' pointer initializer is not null > 5 | constexpr char * s[] = {"a", "b"}; > > > Then we were asking why constexpr was used in that case. Why not? When I compile that with g++ 14.1.0, with -std=c++23, I get: warning: ISO C++ forbids converting a string constant to ‘char*’ [-Wwrite-strings] When I replace "char*" by "const char*", that warning goes away. The initializer *can* be evaluated at compile time. Using constexpr catches any changes to the initializer that would require run-time code execution. Feel free to redirect to comp.lang.c++ if you want to discuss the C++ aspects further. -- Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com void Void(void) { Void(); } /* The recursive call of the void */