C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Floating idea for reserve overload

From: Thiago Macieira <thiago_at_[hidden]>
Date: Wed, 15 Apr 2026 20:07:07 -0700
On Wednesday, 15 April 2026 18:31:41 Pacific Daylight Time Evan Teran via Std-
Proposals wrote:
> Today, users who want to avoid conversion warnings (which I personally
> consider a best practice) must manually convert from signed to unsigned,
> often after performing explicit negative checks which are easy to forget to
> do. I would propose it as a template restricted to std::signed_integral so
> the extra cost of the negative value check is only paid for signed types
> and to avoid ambiguity with the existing size_type based implementation.

The new overload won't help you if you need to check for negative values. If
you made a calculation that could have become negative, you need to check it
anyway because you've done something wrong and it shouldn't be on the called
function to decide what to do. And what would it do: ignore doing nothing, or
be the same as shrink_to_fit()?

While I think (and many people agree) that all this API should have used
signed integers in the first place, adding a few now is a can of worms I doubt
LWG will want to open. Why not add to resize()? How about at() and operator[]?

-- 
Thiago Macieira - thiago (AT) macieira.info - thiago (AT) kde.org
  Principal Engineer - Intel Data Center - Platform & Sys. Eng.

Received on 2026-04-16 03:07:16