The current guideline is to use std::unique_ptr or other smart
pointer, and not raw pointers. So by that argument we have already
achieved what you appear to be trying to do by having a guideline.
Smart pointers don't suffer, use after free or double free problems, so obviously I'm talking about low level code dealing with raw pointers.
> Yes, and that's the point of it, as soon as the originating pointer is freed then all references are nullptr, thus unusable.
> And if one of the references is freed, automatically the originating pointer gets nullified.
I didn't say that the originating pointer is deleted/freed, I said its
lifetime ends. The pointer, not what it's pointing at.
void somefn() {
T * subthing = new T;
// manipulate *subthing, perhaps some calculations
somevec.push_back(BiggerThing{subthing});
}
The BiggerThing would have had to take the T* by reference, and store
that T* reference in itself. BiggerThing gets put in a vector, and
thus its lifetime is now longer than the execution of somefn (lets
ignore the complications of what a member variable which is a
reference introduces). Somefn ends and subthing ends its lifetime,
rendering the reference stored in BiggerThing now dangling.
Heck, that guideline would break std::unique_ptr by itself. That
guideline would have unique_ptr take the pointer in by reference,
store that reference, and pretty much by intention the originating
pointer ends its lifetime. Never mind that one would not be able to
move the unique_ptr since you can't re-seat the reference its storing,
so the originating unique_ptr could not be reset back to nullptr, it's
forced to continue to reference the originating pointer (which is no
longer in its lifetime, so the reference is dangling and attemtping to
use it is UB anyway!).
You are mixing between raw pointers usage and smart pointers usage.
Passing by copy, creates resources ownership confusion.
Passing the pointer variable by reference imposes on it to outlive all the references, but at the same time, if any of these variables is a parameter to the delete operator, then it's guaranteed to become nullptr as per the proposal, so no false ownership.
that's the guideline purpose.
Let's forget the passing by reference thing....just focus on the delete operator which nullify it's pointer parameter. I don't see why it is not the standard..