C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Consistent behavior on Container actions

From: Thiago Macieira <thiago_at_[hidden]>
Date: Fri, 24 Oct 2025 19:23:25 -0700
On Friday, 24 October 2025 17:01:46 Pacific Daylight Time Jonathan Wakely via
Std-Proposals wrote:
> Why?
>
> Your mental model is not how it works, why would we complicate things to
> support an incorrect model?

Agreed, it seems to apply a pedantic purism of what a container should do,
instead of being practical. The example of moving is telling: technically,
moving one container to another could just move all elements - it is what a
container using Small Container Optimisation (like std::string) does. But in
practice, doing that for arbitrarily-sized containers is just wasteful,
because the operation can in many cases be achieved in O(1), and in the worst
it's an O(n) destruction of the target container's elements.

Non-move assignment usually does try to assign element by element, provided
the target container has that many elements. The question is what happens when
it has fewer: should the container default-construct elements just so they be
assigned to? That might be correct, but it's wasteful.

And if someone *really* needs that, they can just ensure the target container
has the right size before the assignment. There's no need to change anything
in the library.

-- 
Thiago Macieira - thiago (AT) macieira.info - thiago (AT) kde.org
  Principal Engineer - Intel Data Center Group

Received on 2025-10-25 02:23:31