C++ Logo

std-proposals

Advanced search

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

From: Jason C <jason.cipriani_at_[hidden]>
Date: Tue, 20 Dec 2022 04:46:09 -0500
On Tue, Dec 20, 2022 at 2:07 AM Frederick Virchanza Gotham via
Std-Proposals <std-proposals_at_[hidden]> wrote:

> On Tue, Dec 20, 2022 at 7:00 AM connor horman via Std-Proposals
> <std-proposals_at_[hidden]> wrote:
>
> > TBH, delete p = nullptr; reads too much like delete (p = nullptr);, even
> if it doesn't parse that way,
> > that I wouldn't ever want to use it, or attempt reading code that uses
> it.
>
> Therefore when you see "delete p = nullptr;", irrespective of whether
> you're familiar with operator
> precedence, you know that it must be setting the pointer to null after
> delete'ing it, because nothing
> else makes sense.
>

a = b = 3 and a = (b = 3) are equivalent.

It would be confusing for all involved if delete a = nullptr and delete (a
= nullptr) were not equivalent.

Also C++ requires that B be sequenced before A in the statement (A = B), so
it's not really clear how to formally make "delete" on the left side have
precedence over assignments on the right side.

As an alternative to std::unique_ptr or std::shared_ptr, you can always
wrap these sorts of things in macros or a short utility function as well,
if needed.

Received on 2022-12-20 09:46:37