C++ Logo


Advanced search

Subject: Is string reserve() bound to do anything useful?
From: Adam Badura (adam.f.badura_at_[hidden])
Date: 2021-01-14 01:54:07

I was reading the specification regarding what string reserve() is required
to do and what it ensures. And as far as I can tell, it doesn't really
guarantee what is popularly expected (like that there will be no
reallocation if you do not cross the capacity).

As far as I understand the specification, a conforming basic_string
implementation could very well keep a capacity member value (it must since
the value is observable with the capacity() method) and keep growing it
upon calls to reserve or changes of the string size but otherwise ignore it.

The N4861 (https://timsong-cpp.github.io/cppwp/n4861/, C++20 I assume)
improves a bit since in [string.capacity] (
point 9 we can read about "reallocation". This hints at the expected
meaning. However, the term "reallocation" doesn't seem to be clarified
anywhere. So, again, it is not really specified.

Am I too picky about the wording? Or is it really poorly expressed?

Would it be feasible to change the wording of basic_string (and others as
well - I expect containers have the same issue) to be more formal and clear
about dependencies between size, capacity, and calls to the allocator. Also
regarding exception guarantees - if you operate on the string within its
capacity the allocator is not called and hence all operations are noexcept.
(Provided there are no index errors etc. which would otherwise lead to
exceptions anyway.)

Adam Badura

STD-DISCUSSION list run by std-discussion-owner@lists.isocpp.org

Older Archives on Google Groups