C++ Logo

std-proposals

Advanced search

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

From: organicoman <organicoman_at_[hidden]>
Date: Sun, 24 Aug 2025 12:12:04 +0100
auto val = v[1];creates a copy and does not store a reference even though a reference was returned. You have to writeauto &val = v[1];That's what I referred to as unintentional error.One character missing and the meaning changes, now compare it to your example snippet. And it is certainly a good idea that operator[] returns a reference because you can also store really large objects inside containers. You might even want to be able to writev[1] = 2;Same function with two actions, that breaks the single responsibility principle. which is only possible with a reference.A reference also means you can put non-copyable objects inside the container, That's certainly a bad decision, and I advice against that.like unique_ptr. You see, a reference is almost mandatory here for C++ to succeedIf the designer of the type explicity express that his type is not copiable, why fight with his design, and use references?All what you've said so far can be answered with good argument, but let's not deviate from the main idea.One more additional thing: If you assign a new address to a pointer after it has been deleted and you never read from that pointer again, the compiler is allowed to optimize away this assignment. It does not matter that others have a reference to it. There is currently no legal way to write the kind of code in C++ and actually do what you want. (And disallowing this optimization will not get you any friends.) This is why this belongs into the compiler as a flag because you cannot express it in the language.This paragraph is the most interesting. I need to investigate this.

Received on 2025-08-24 11:12:26