This is the mail archive of the
libstdc++@gcc.gnu.org
mailing list for the libstdc++ project.
Re: std::string::reserve vs std::vector::reserve
- From: Jonathan Wakely <cow at compsoc dot man dot ac dot uk>
- To: Peter Soetens <peter dot soetens at mech dot kuleuven dot be>
- Cc: libstdc++ at gcc dot gnu dot org
- Date: Thu, 16 Jun 2005 12:21:39 +0100
- Subject: Re: std::string::reserve vs std::vector::reserve
- References: <200506161300.37570.peter.soetens@mech.kuleuven.be>
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