Deutsch   English   Français   Italiano  
<87a5kd1n54.fsf@nosuchdomain.example.com>

View for Bookmarking (what is this?)
Look up another Usenet article

Path: ...!2.eu.feeder.erje.net!feeder.erje.net!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: C23 thoughts and opinions
Date: Sat, 25 May 2024 16:34:47 -0700
Organization: None to speak of
Lines: 87
Message-ID: <87a5kd1n54.fsf@nosuchdomain.example.com>
References: <v2l828$18v7f$1@dont-email.me>
	<00297443-2fee-48d4-81a0-9ff6ae6481e4@gmail.com>
	<v2lji1$1bbcp$1@dont-email.me>
	<9be0c55c-38f8-496c-b335-84ad281e1753@gmail.com>
	<v2nc5h$1oban$1@dont-email.me>
	<c5866b5a-0314-4e70-af56-a86b63986b0c@gmail.com>
	<v2nfai$1ougd$1@dont-email.me>
	<cf267279-e9cf-415f-898c-f5830a997529@gmail.com>
	<87ed9s42sb.fsf@nosuchdomain.example.com>
	<f5774ff3-d2b0-4787-b16c-3b193dc418ef@gmail.com>
	<87bk4v2du1.fsf@nosuchdomain.example.com>
	<ec20f752-6132-40f5-a4e8-f884746d2f3a@gmail.com>
	<87v8330xq3.fsf@nosuchdomain.example.com>
	<97c17a49-a940-42ff-83b8-df755fb6e88e@gmail.com>
	<87r0dq1zxf.fsf@nosuchdomain.example.com>
	<b7704352-e11c-49b7-9fe5-3435e5f7e2bc@gmail.com>
	<v2stuf$2tphj$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit
Injection-Date: Sun, 26 May 2024 01:34:48 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="f86e0e741270de1a6fdb7e3596a53ea0";
	logging-data="3247459"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX1+4b0ysdi/9A9hWTUaICzh7"
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux)
Cancel-Lock: sha1:UUA5Tnw5KbUkzveI6hfSQdvU1Kk=
	sha1:DY5dJcfDULj4SCBVC01brUmym5Y=
Bytes: 5287

David Brown <david.brown@hesbynett.no> writes:
> On 25/05/2024 13:33, Thiago Adams wrote:
>> Em 5/24/2024 9:46 PM, Keith Thompson escreveu:
>>> Thiago Adams <thiago.adams@gmail.com> writes:
>>>> Em 5/24/2024 5:19 PM, Keith Thompson escreveu:
>>>>> Thiago Adams <thiago.adams@gmail.com> writes:
>>>>>> On 24/05/2024 16:45, Keith Thompson wrote:
>>>>>>> Thiago Adams <thiago.adams@gmail.com> 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 <stdio.h>
>>>> #include <stdlib.h>
>>>> 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"?
>>>
>> Something used to produce a constant expression.
>> In the loop the compiler would have to get the value in runtime from
>> array, or unroll the loop.
>> I just checked, trying to extract an constant value from the array
>> 
>> https://godbolt.org/z/v33Pqd7W8
>> #include <stdio.h>
>> #include <stdlib.h>
>> int main() {
>>      constexpr int a[] = {1, 2, 3, 4, 5, 6, 7, 8};
>>      static_assert(a[0] ==1 );
>> }
>> I was expecting this to work!
>> But gcc says
>> <source>:5:24: error: expression in static assertion is not constant
>>      5 |     static_assert(a[0] ==1 );
>>        |
>> 
>
> That is disappointing.  I too would have expected that to work in
> C23. My guess is that it is the implicit pointer dereference that is
> the problem.  But I hope this is something that gets fixed shortly.
[...]

The definition of constant expressions in C23 isn't much different from
the definition in C17.  I think the committee was cautious about making
too many changes.  Adding the full semantics of C++'s constexpr likely
would have been impractical.

Don't expect this to be "fixed" before C26 at the earliest.

-- 
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
void Void(void) { Void(); } /* The recursive call of the void */