Path: ...!news.mixmin.net!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Ben Bacarisse Newsgroups: comp.lang.c Subject: Re: technology discussion =?utf-8?Q?=E2=86=92?= does the world need a "new" C ? Date: Mon, 19 Aug 2024 14:14:35 +0100 Organization: A noiseless patient Spider Lines: 102 Message-ID: <87frr0r6x0.fsf@bsb.me.uk> References: <20240712154252.00005c2f@yahoo.com> <86o7717jj1.fsf@linuxsc.com> <20240717163457.000067bb@yahoo.com> <86a5hep45h.fsf@linuxsc.com> <87y14xsvnh.fsf@bsb.me.uk> <87sev5s51s.fsf@bsb.me.uk> <87jzggss6h.fsf@bsb.me.uk> <8734n1s7z8.fsf@bsb.me.uk> <865xrxe32q.fsf@linuxsc.com> MIME-Version: 1.0 Content-Type: text/plain Injection-Date: Mon, 19 Aug 2024 15:14:35 +0200 (CEST) Injection-Info: dont-email.me; posting-host="153c0803c54c022691c586705843dea6"; logging-data="3024294"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/GzikTIMcemh4+DRrOhOjSxM99ZZC8YJ8=" User-Agent: Gnus/5.13 (Gnus v5.13) Cancel-Lock: sha1:j1Tk828XW/KAHdNK35br6yPOFOk= sha1:3UkY1aJT8qDrCbrR8iBT2vn5rrM= X-BSB-Auth: 1.6cac499961287adb41e8.20240819141435BST.87frr0r6x0.fsf@bsb.me.uk Bytes: 6176 David Brown writes: > On 19/08/2024 03:03, Tim Rentsch wrote: >> Ben Bacarisse writes: >> >>> David Brown writes: >>> >>>> On 16/08/2024 12:00, Ben Bacarisse wrote: >>>> >>>>> David Brown writes: .... >>>>>> In C++, you can't pass arrays as parameters at all - the language >>>>>> inherited C's handling of arrays. You can, of course, pass >>>>>> objects of std::array<> type by value or by reference, just like >>>>>> any other class types. >>>>> >>>>> The best way to think about C++ (in my very non-expert opinion) is >>>>> to consider references as values that are passed by, err..., >>>>> value. But you seem prepared to accept that some things can be >>>>> "passed by reference" in C++. >>>> >>>> That seems a subtle distinction - I'll have to think about it a >>>> little. I like your description of arguments being like local >>>> variable initialisation - it makes sense equally well regardless of >>>> whether the parameter is "int", "int*", or "int&". (It's probably >>>> best not to mention the other one in this group...) >>>> >>>>> So if this: >>>>> #include >>>>> void g(int &i) { std::cout << i << "\n"; } >>>>> int main(void) >>>>> { >>>>> int I{0}; >>>>> g(I); >>>>> } >>>>> shows an int object, I, being passed to g, why does this >>>>> #include >>>>> void f(int (&ar)[10]) { std::cout << sizeof ar << "\n"; } >>>>> int main(void) >>>>> { >>>>> int A[10]; >>>>> f(A); >>>>> } >>>>> not show an array, A, being passed to f? >>>> >>>> That's backwards compatibility with C array handling at play. >>> >>> I'm not sure how this answers my question. Maybe you weren't >>> answering it and were just making a remark... >> My guess is he didn't understand the question. The code shown >> has nothing to do with backwards compatibility with C array >> handling. > > I had intended to make a brief remark and thought that was all that was > needed to answer the question. But having thought about it a bit more > (prompted by these last two posts), and tested the code (on the assumption > that the gcc writers know the details better than I do), you are correct - > I did misunderstand the question. I was wrong in how I thought array > reference parameters worked in C++, and the way Ben worded the question > re-enforced that misunderstanding. I'm sorry I wasn't more clear. > I interpreted his question as saying that the code "f" does not show an > array type being passed by reference, with the implication that the > "sizeof" showed the size of a pointer, not the size of an array of 10 ints, > and asking why C++ was defined that way. The answer, as I saw it, was that > C++ made reference parameters to arrays work much like pointer parameters > to arrays, and those work like in C for backwards compatibility. Ah. Then is was an answer to the question you thought I was asking. The trouble is, it never occurred to me you would not know that C++ array references behave just like all other references -- as aliases to the objects they reference. So I was asking for some explanation of how you were using terms: "if this is 'passing an int', in what sense is this not 'passing an array'?". (I hope you don't think I'm being rude. C++ is a gruesomely huge language, littered with special cases and array reference must be almost never used, especially now. But I started with the first release of Cfront, so I learned all the primitive bits first. It's the new bits I can't fathom.) > Of course, it turns out I was completely wrong about how array type > reference parameters work in C++. It's not something I have had use for in > my own C++ programming or something I have come across in other code that I > can remember, and I had made incorrect assumptions about it. Now that I > corrected that, it all makes a lot more sense. > > And so I presume Ben was actually asking why I /thought/ this was not > passing an array type (thus with its full type information, including its > size). Then answer there is now obvious - I thought that because I had > jumped to incorrect conclusions about array reference parameters in > C++. Well, it was little more like I thought you were maybe using the term "passing by reference" is some way that I'd missed. That's why I started with a brief explanation of how I used to explain it. -- Ben.