Deutsch   English   Français   Italiano  
<vf2q0k$c52l$4@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 13:36:51 +0200
Organization: A noiseless patient Spider
Lines: 79
Message-ID: <vf2q0k$c52l$4@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> <vf1216$p0c$1@dont-email.me>
 <vf2n0q$c52l$2@dont-email.me> <vf2p7t$ci0l$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 13:36:52 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="e075af0a451a2884c4ee8f8ff13dce24";
	logging-data="398421"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX18q3mQOCy1tNRt+kXgEEtjApDIFhbcl9Io="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:pkWZjRl7iye+fVammfG1TaQih1I=
Content-Language: en-GB
In-Reply-To: <vf2p7t$ci0l$1@dont-email.me>
Bytes: 3984

On 20/10/2024 13:23, Bart wrote:
> On 20/10/2024 11:45, David Brown wrote:
>> On 19/10/2024 21:41, Thiago Adams wrote:
>>> Em 10/19/2024 1:03 PM, David Brown escreveu:
>>>> 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? 
>>>
>>> I don't see any practical difference. In theory, the generated code 
>>> could be different, but I'm arguing that this doesn't really matter 
>>> and, consequently, it's not a good reason to differentiate between 
>>> const and constexpr.
>>>
>>
>> My point was that if there is no practical difference, then there is 
>> no reason to object to the VLA.
>>
> I've seen endless exampples where people inadvertently created VLAs, and 
> where they are likely to less efficient.
> 

I can't say I have ever seem such as situation.  But of course 
experiences differ.

> It might start off like this:
> 
>      const int n = 10;
>      int A[n];
> 
> Then they change something so that n is not evaluated until runtime 
> (maybe it's defined in terms of a parameter). Now the compiler will 
> silently generate less efficient code for a VLA, without giving the user 
> a chance to use an alternative.
> 

Yes, a VLA where the size is not known at compile time will be less 
efficient than when the size is fixed.  It might still be a lot more 
efficient than alternatives, such as using malloc().  I think VLAs - 
with run-time sizes - have their uses, but you should be aware of when 
you use them.

> 
>> You can't use this as a reason for arguing that it would have been 
>> better for "const" in C to gain the features that are now in C23 
>> "constexpr", because this use of "const" was already allowed in C99.  
>> So the "const" vs "constexpr" discussion is an orthogonal issue - I 
>> was asking specifically about your comment regarding your apparent 
>> dislike of VLA's.
> 
> The advantage of constexpr AIUI is that a non-constant initialiser for n 
> is not allowed.
> 

Yes, that is something I also see as an advantage.