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:23:25 -0400
On Sat, Oct 1, 2022 at 10:10 AM blacktea hamburger via Std-Proposals
<std-proposals_at_[hidden]> 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.

Then I would say that this is a defect in the standard. `operator
new`+placement-`new T` is *not* identical to `new T` for all types
`T`. As Thiago mentioned, if `T` overloads `operator new/delete`, then
this will cause a problem when you try to use the `delete` expression
instead of doing the proper inverse operation (ie: explicit destructor
call followed by `operator delete`).

It doesn't make sense to use two-stage object creation but one-stage
object destruction. Especially when there are cases where it
explicitly will not work. Better to forbid them all the time than to
encourage people to roll the dice.

> The standard should not forbid some code that is not very good, but correct.

Um... why not?

Received on 2022-10-01 16:24:42