C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Proposal: Clarifying or Deprecating clear() in STL containers

From: Arthur O'Dwyer <arthur.j.odwyer_at_[hidden]>
Date: Wed, 8 Oct 2025 16:32:28 -0400
On Wed, Oct 8, 2025 at 4:30 PM Marcin Jaczewski <marcinjaczewski86_at_[hidden]>
wrote:

> wt., 7 paź 2025 o 23:10 Arthur O'Dwyer via Std-Proposals <
> std-proposals_at_[hidden]> napisał(a):
> >
> > We obviously can't deprecate `clear()`, nor change its behavior by fiat.
> That ship sailed very long ago, and `clear()` is a very widely used method.
> >
> > However, you're right that its semantics are generally underspecified
> and there is (very slight) vendor divergence:
> >
> https://quuxplusone.github.io/blog/2025/01/02/shrink-to-fit/#how-do-i-clear-and-free-in-one-line
> >
> > And you're right that in industry code it's usually desirable to (1)
> have a specific idiom for clearing-and-deallocating, [...]


> Question is would be `x = {};` prefered way to clear the container?
>

https://quuxplusone.github.io/blog/2025/01/02/shrink-to-fit/#how-do-i-clear-and-free-in-one-line


> It should move the temp object that is empty to the current one.
>

No, that's not what `x = {};` does at all.

–Arthur

Received on 2025-10-08 20:32:45