Date: Fri, 22 Aug 2025 21:49:09 -0700
> Impeding a language feature to guarantee the safety, is not really practical. So it's better to provide a guideline.
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.
> > I guess a third problem: since everything is taken by reference (by the guideline) then the originating pointer has to have a longer lifetime than any of its “copies”.
>
> 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!).
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.
> > I guess a third problem: since everything is taken by reference (by the guideline) then the originating pointer has to have a longer lifetime than any of its “copies”.
>
> 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!).
Received on 2025-08-23 04:49:21