[v3] basic_string::reserve() shrink-to-fit?

Paolo Carlini pcarlini@unitus.it
Thu Dec 11 17:33:00 GMT 2003


On Thu, Dec 11, 2003 at 09:08:33AM -0800, Nathan Myers wrote:

>>> e. reserve() doesn't seem to shrink the string capacity to fit.
>>
>> I'm afraid that's what's supposed to happen, in basic_string<> and
>> in vector<>. The idiom for shrinking to fit is to use swap().
>> Minimally,
>>
>> std::swap(std::string(str.data(), str.size()), str);
>>
>> This might be a good thing to put in the FAQ.
>
Hummm...

Now I have the standard here, and in my reading of 21.3.3, p9-11, seems
definitely allowed for a reserve with argument <= capacity at the moment
of the call, to shrink below the capacity! p11 says that: "Effects: After
reserve(), capacity() is greater or equal to the argument of reserve.
[Note: Calling reserve() with a res_arg argument less than capacity() is
in effect a non-binding shrink request. A call with res_arg <= size() is
in effect a non-binding shrink-to-fit request]"

Well, my english is not so good, but the espression "non-binding" doesn't
seem to imply that an implementation *cannot* actually shrink below the
old capacity, quite to the contrary!

Nathan?

Paolo.




More information about the Libstdc++ mailing list