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: std::string::reserve vs std::vector::reserve


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



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