Path: ...!weretis.net!feeder8.news.weretis.net!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: Fri, 24 May 2024 17:46:20 -0700 Organization: None to speak of Lines: 74 Message-ID: <87r0dq1zxf.fsf@nosuchdomain.example.com> References: <00297443-2fee-48d4-81a0-9ff6ae6481e4@gmail.com> <9be0c55c-38f8-496c-b335-84ad281e1753@gmail.com> <87ed9s42sb.fsf@nosuchdomain.example.com> <87bk4v2du1.fsf@nosuchdomain.example.com> <87v8330xq3.fsf@nosuchdomain.example.com> <97c17a49-a940-42ff-83b8-df755fb6e88e@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit Injection-Date: Sat, 25 May 2024 02:46:21 +0200 (CEST) Injection-Info: dont-email.me; posting-host="abfb02867fd1fd48387abe9897b36cd1"; logging-data="2698832"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18NgisvZuSfd9kApi9PIoBK" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux) Cancel-Lock: sha1:lvx6CKk5NwQny/Q2mvGaJbXXnz0= sha1:wYVCrHmUC+vvZpIdptbkj6WGl+0= Bytes: 4760 Thiago Adams writes: > Em 5/24/2024 5:19 PM, Keith Thompson escreveu: >> Thiago Adams writes: >>> On 24/05/2024 16:45, Keith Thompson wrote: >>>> Thiago Adams writes: >>>>> On 23/05/2024 18:49, Keith Thompson wrote: >>>>>>> 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 see a constexpr I ask if the compiler is able to compute >>>>> everything at compile time. If not immediately it is a bad usage in my >>>>> view. >>>> I don't understand. Do you object because it's not *immediately >>>> obvious* that everthing can be computed at compile time? If so, why >>>> should it have to be? >>> >>> My understanding is that constexpr is a tip for the compiler. Does not >>> ensure anything. Unless you use where constant expression is required. >>> So I don't like to see constexpr where I know it is not a constant >>> expression. >> Your understanding is incorrect. "constexpr" is not a mere hint. > I think I can explain I little better > > Let´s consider we have a compile time array of integers and a loop. > > https://godbolt.org/z/e8cM1KGWT > > #include > #include > int main() { > constexpr int a[] = {1, 2, 3, 4, 5, 6, 7, 8}; > for (int i = 0 ; i < sizeof(a)/sizeof(a[0]); i++) > { > printf("%d", a[i]); > } > } > > What the programmer expected using a constant array in a loop? > The loop is in runtime, unless the compiler expanded the loop into 8 > calls using constant expressions. But this is not the case. > This was the usage of constexpr I saw but with literal strings. > So, the array a is not used as constant even if it has constexpr. What do you mean by "used as constant"? The "constexpr" in the above asserts that the initializer for a consists of constant expressions. Perhaps there's a reason that's desirable. Perhaps the initial values come from `#embed`. a[3] is a constant expression, as I understand it; a[i] is not. The code that accesses `a` doesn't depend on the fact that it's constant -- but the code still works, and the constexpr is harmless. Reading the code, you can be certain that, assuming it compiles, the value is determined at compile time. You don't necessarily have to make use of that information. If you see int n = 42; you know that the value of 42 is determined at compile time, even if you don't make use of that knowledge. My preference is to mark declared objects as `const` (read-only) unless there's a specific reason not to (I need to modify it). Some more recent languages make the equivalent of `const` the default, and require a qualifier to make an object modifiable. I really like that (though adding that to C would be impractical). I'm thinking that I'll probably have the same preference for `constexpr`. -- Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com void Void(void) { Void(); } /* The recursive call of the void */