C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Relax the restriction on the operand of a single-object delete-expression

From: Jason McKesson <jmckesson_at_[hidden]>
Date: Sat, 1 Oct 2022 12:19:01 -0400
On Sat, Oct 1, 2022 at 11:45 AM Thiago Macieira via Std-Proposals
<std-proposals_at_[hidden]> wrote:
>
> On Saturday, 1 October 2022 07:09:47 PDT blacktea hamburger via Std-Proposals
> wrote:
> > > For example:
> > >
> > > ```
> > > auto *p = operator new(1);
> > > auto *pc = new(p) char;
> > > delete pc;
> > > ```
> > >
> > > That delete is invalid, and it *should be*. If you use two-step object
> > > creation (allocate memory, then put an object into it), you should use
> > > two-step object destruction (destroy the object, then deallocate the
> > > memory).
> >
> > In fact, it is legal. The standard does not specify that a new-expression
> > cannot be a placement-new.
> >
> > The standard should not forbid some code that is not very good, but correct.
>
> I agree it's valid, so long as the type we are talking about is really char
> here. It's only correct so long as the type in question doesn't have an
> overridden operator new / delete, and primitives can't have them overridden.

Of course, implicit lifetime types are not prevented from having
overloaded `operator delete`. I mean, why would they be? IOC is about
creating the object within existing storage. Overloaded `operator
delete` is about how such types deallocate storage when you
allocate&create them with `new`. Creating them in other ways does not,
and should not, use the `operator delete`.

Received on 2022-10-01 16:20:18