std::string::reserve vs std::vector::reserve

Jonathan Wakely cow@compsoc.man.ac.uk
Thu Jun 16 11:22:00 GMT 2005


On Thu, Jun 16, 2005 at 01:00:36PM +0200, Peter Soetens wrote:

> Hi,
> 
> I do not have the Standard, but the "C++ Programming Language" by BS states 
> that both string reserve and operator= behave 'like' vector's. Well, in GCC 
> 2.95, 3.2, 3.3,3.4 and 4.0, it doesn't (so I'm probably wrong...).
> 
> A vector keeps its reserved capacity after an assignment, a string doesn't. 
> 
> I know this is because of the COW implementation. In fact, the string gets the 
> capacity of the original... How does the Standard define the correct 
> behaviour ?

The standard was written explicitly so that COW strings would be
possible (some implementors have since objected that it's worded to make
non-ref-counted strings too difficult!)

The only requirement on std::string::operator= is that capacity() should
return "a value at least as large as size()", which libstdc++'s
implementation does.

>             Will one day vector behave likewise ?

No. Absolutely not.

jon




More information about the Libstdc++ mailing list