Deutsch   English   Français   Italiano  
<20240717163457.000067bb@yahoo.com>

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

Path: ...!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Michael S <already5chosen@yahoo.com>
Newsgroups: comp.lang.c
Subject: Re: technology discussion =?UTF-8?Q?=E2=86=92?= does the world need
 a "new" C ?
Date: Wed, 17 Jul 2024 16:34:57 +0300
Organization: A noiseless patient Spider
Lines: 94
Message-ID: <20240717163457.000067bb@yahoo.com>
References: <v66eci$2qeee$1@dont-email.me>
	<v6jiud$1dsjb$1@dont-email.me>
	<877cdur1z9.fsf@bsb.me.uk>
	<v6joi4$1epoj$1@dont-email.me>
	<871q42qy33.fsf@bsb.me.uk>
	<v6k6i0$1h4d3$1@dont-email.me>
	<87ed82p28y.fsf@bsb.me.uk>
	<v6m03l$1tf05$1@dont-email.me>
	<87r0c1nzjj.fsf@bsb.me.uk>
	<v6m716$1urj4$1@dont-email.me>
	<87ikxconq4.fsf@bsb.me.uk>
	<v6n8iu$24af0$1@dont-email.me>
	<20240711115418.00001cdf@yahoo.com>
	<v6oamt$2d8nn$1@dont-email.me>
	<v6oct4$2djgq$2@dont-email.me>
	<v6of96$2ekb0$1@dont-email.me>
	<v6ovfc$2hcpf$1@dont-email.me>
	<v6p4hf$2icph$1@dont-email.me>
	<v6qgpu$2t6p7$3@dont-email.me>
	<v6r33m$30grj$1@dont-email.me>
	<20240712154252.00005c2f@yahoo.com>
	<86o7717jj1.fsf@linuxsc.com>
	<v6ti10$3gru4$1@dont-email.me>
	<v78af7$1qkuf$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=US-ASCII
Content-Transfer-Encoding: 7bit
Injection-Date: Wed, 17 Jul 2024 15:34:30 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="559f6b365bf6c250c47308681bc81cd1";
	logging-data="1925496"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX1/wKFraUpxls09li91Ve1twCUkl+0wqos0="
Cancel-Lock: sha1:BdXiXh6LpOlZ2hwInFTWvMYtM6k=
X-Newsreader: Claws Mail 3.19.1 (GTK+ 2.24.33; x86_64-w64-mingw32)
Bytes: 4781

On Wed, 17 Jul 2024 12:38:15 +0100
Bart <bc@freeuk.com> wrote:

> On 13/07/2024 10:39, BGB wrote:
> 
> > But, as I see it, no real point in arguing this stuff (personally,
> > I have better stuff to be doing...).  
> 
> We all do. But this group seems to be about arguing about pointless 
> stuff and you might come here when you want a respite from proper
> work.
> 
> However (here I assume you've gone back to Quake but that other 
> interested parties might be reading this), consider the program below.
> 
> That sets up an array and then sums its elements by calling 3
> different functions to do the job:
> 
> (1) Using normal C pass-by-value
> 
> (2) Using C pass-by-value to emulate call-by-reference
> 
> (3) Using fantasy true call-by-reference as it might appear if C had
> the feature
> 
> (I'd hoped C++ would run this, but it didn't even like the middle
> function.)
> 
> I'm asking people to compare the first and third functions and their 
> calls, and to see if there's any appreciable difference between them. 
> There will obviously be a difference in how the A parameter is
> declared.
> 
> ---------------------------------------------
> #include <stdio.h>
> 
> typedef int T;
> 
> int sum_byvalue(T* A, int n) {
>      int i, sum=0;
>      for (i=0; i<n; ++i) sum += A[i];
>      return sum;
> }
> 
> int sum_bymanualref(T(*A)[], int n) {
>      int i, sum=0;
>      for (i=0; i<n; ++i) sum += (*A)[i];
>      return sum;
> }
> 
> int sum_bytrueref(T (&A)[], int n) {
>      int i, sum=0;
>      for (i=0; i<n; ++i) sum += A[i];
>      return sum;
> }
> 
> int main(void) {
>      enum {N = 10};
>      T A[N] = {10,20,30,40,50,60,70,80,90,100};
>      int total=0;
> 
>      total += sum_byvalue     (A, N);
>      total += sum_bymanualref (&A, N);
>      total += sum_bytrueref   (A, N);
> 
>      printf("%d\n", total);             // would show 1650
> }
> ---------------------------------------------
> 
> Find anything? I thought not.
> 
> Those findings might suggest that C doesn't need call-by-reference,
> not for arrays anyway. Except that at present you can do this:
> 
>      T x=42;
>      sum_byvalue(&x, N);
> 
> which would not be possible with call-by-reference. Nor with 
> sum_bymanualref, but apparently nobody wants to be doing with all
> that extra, fiddly syntax. Better to be unsafe!

The C++ syntax your are looking for is sum_bytrueref(std::array<T,10>&A,
And indeed, the generated code is the same.
https://godbolt.org/z/dYGoWsdjE
But why is it the same? Because in C++ arrays are also 2nd class
citizen, like in C ! std::array is not a 'native' C++ type, but a
wrapper around array-within-struct pattern.
The proper comparison would be vs language that has arrays as 1st class
citizen. 

I tried to produce Ada example on Godbolt, but it seems that support
for Ada on GB is too limited. I was not able to convince it to compile
package. And without packages I was not able to illustrate my point.