Date: Sat, 23 Aug 2025 09:21:55 +0100
On Sat, 23 Aug 2025, 06:59 organicoman via Std-Proposals, <
std-proposals_at_[hidden]> wrote:
>
>
>
>
> Actually it does work without breaking any code.
> You have to overload the delete operator in global scope.
>
> template <typename T>
> void delete(void*, T* &p)
> {
> delete p;
> p= nullptr;
> }
>
> And you do the same with the delete[ ].
>
This is not necessary.
When you write `delete p` the compiler already does more than just call
`operator delete(p)`, because it also destroys `*p`
The compiler can easily turn `delete p` into:
std::destroy_at(p);
operator delete(p);
p = nullptr;
This is already allowed today, you don't need to change the standard. It's
not*required* by the standard because it adds a store that is usually
redundant, but compilers could offer an option to do this.
As others have pointed out, doing that is unlikely to prevent very many
bugs in practice.
> But because there is always a chance that some class will reimplement
> these operators, then the behavior is not uniform, unless the standard
> delete operator does the trick.
>
That doesn't matter. Zeroing the pointer can be done by the delete
operator, not by the `operator delete` function.
std-proposals_at_[hidden]> wrote:
>
>
>
>
> Actually it does work without breaking any code.
> You have to overload the delete operator in global scope.
>
> template <typename T>
> void delete(void*, T* &p)
> {
> delete p;
> p= nullptr;
> }
>
> And you do the same with the delete[ ].
>
This is not necessary.
When you write `delete p` the compiler already does more than just call
`operator delete(p)`, because it also destroys `*p`
The compiler can easily turn `delete p` into:
std::destroy_at(p);
operator delete(p);
p = nullptr;
This is already allowed today, you don't need to change the standard. It's
not*required* by the standard because it adds a store that is usually
redundant, but compilers could offer an option to do this.
As others have pointed out, doing that is unlikely to prevent very many
bugs in practice.
> But because there is always a chance that some class will reimplement
> these operators, then the behavior is not uniform, unless the standard
> delete operator does the trick.
>
That doesn't matter. Zeroing the pointer can be done by the delete
operator, not by the `operator delete` function.
Received on 2025-08-23 08:22:12