C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Delete...why not a parameter by reference?!

From: Simon Schröder <dr.simon.schroeder_at_[hidden]>
Date: Mon, 25 Aug 2025 08:15:47 +0200
> On Aug 24, 2025, at 10:35 PM, organicoman via Std-Proposals <std-proposals_at_[hidden]> wrote:
>
> Very correct, that's why I didn't deviate into that with Simon.
> Also he mentioned indirection and inefficiency of passing by reference,....well if a compiler can optimize a non used variable, definitely it can collapse a reference into a pointer.

I think I need to respond to this specifically because it has to do why your proposal has ABI problems. The magic you wish for does not exist. Within a local scope the compiler can do some magic and threat a reference to a variable as an alias to a variable name. This means locally the reference does not have to have its own register or memory location. However, in a function call this is not true anymore. A reference is now more just like a pointer. If you have a reference to a pointer as function argument it cannot be collapsed into just a single pointer. If it could you would not have a reference to the original pointer anymore and thus nulling the pointer inside the function would not communicate it to other pointers. This means the function that takes a reference to a pointer always has to do a double indirection (with optimization at least once). It is up to the library implementer of that function if he favors performance over safety. (The form of safety you are describing can be assured by rigorous testing. Thus the library implementer can write a lot of tests and choose performance.)

Received on 2025-08-25 06:16:00