This is the mail archive of the libstdc++@gcc.gnu.org mailing list for the libstdc++ project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

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


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.



Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]