C++ Logo

std-proposals

Advanced search

Re: [std-proposals] delete p = nullptr;

From: Richard Hodges <hodges.r_at_[hidden]>
Date: Tue, 20 Dec 2022 12:11:57 +0100
On Tue, 20 Dec 2022 at 07:51, Frederick Virchanza Gotham via Std-Proposals <
std-proposals_at_[hidden]> wrote:

> It's common that you'll see the following two lines in C++ code:
>
> delete p;
> p = nullptr;
>
> How about allowing it as follows?
>
> delete p = nulltpr;
>
>
This is already trivial:

template<class T>
void erase(T*& p)
{
delete std::exchange(p, nullptr);
}

int main()
{
auto p = new foo;
assert(p);
erase(p);
assert(!p);
}

>
>

Received on 2022-12-20 11:12:10