Topic: capacity() and reserve() on reference counted strings


Author: Theodore Todorov <Theodore.Todorov@cern.ch>
Date: 1999/08/13
Raw View
John Demars wrote:

> I am working on a class that wraps the string class in the SGI STL
> package (actually the www.stlport.org port of SGI STL), so that the
> wrapper class implements reference counting.
>
> I am puzzled on how a reference counted string should handle capacity()
> and reserve().  Does the standard allow the capacity to decrease when
> the assignment operator (and similar functions) are applied?
>
> For example:
>
> string a("string a");
> a.reserve(1024);
>
> string b();
> a = b;
>

I believe the situation is very similar to std::vector, where an assignment
is guaranteed not to invalidate iterators to the first b.size() elements
of a if a had enough capacity to hold them, and thus the only way
to _reduce_ the capacity of a std::vector is by using it's swap() method.
I am not sure if it's really the case for strings, though.

> If reference counting is used, a will now have b's capacity.
>

That's just perfect. I find the standard behaviour of never-reducing
capacity frustrating, and I can't imagine any situation where
it would be preferable to the one you get with reference counting.




[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Jim Hyslop <jim.hyslop@leitch.com>
Date: 1999/08/13
Raw View
In article <37B076C0.8A3C8EB6@nortelnetworks.com>,
  "John Demars" <demars@nortelnetworks.com> wrote:
> I am working on a class that wraps the string class in the SGI STL
> package (actually the www.stlport.org port of SGI STL), so that the
> wrapper class implements reference counting.
>
> I am puzzled on how a reference counted string should handle
> capacity()
> and reserve().  Does the standard allow the capacity to decrease when
> the assignment operator (and similar functions) are applied?
Yes.  The Standard requires capacity() to return a value "at least as
large as size()" for all the copy ctors and the copy assignment
operator.  This implies that capacity is allowed to shrink.

> For example:
>
> string a("string a");
> a.reserve(1024);
>
> string b();
> a = b;
>
> If reference counting is used, a will now have b's capacity.
Possibly, yes.  Possibly no.  I believe the Standard is flexible on the
question of capacity().  For example, calling reserve() with a value
less than capacity() is a non-binding shrink request - it's up to the
implementation to decide whether or not to honour the request.

You could implement it either way - a will have b's capacity, or a will
have just enough capacity to hold the actual string.

--
Jim
I ignore all email from recruitment agencies.
Please do not send me email with questions - post
here.


Sent via Deja.com http://www.deja.com/
Share what you know. Learn what you don't.


[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: "John Demars" <demars@nortelnetworks.com>
Date: 1999/08/11
Raw View
I am working on a class that wraps the string class in the SGI STL
package (actually the www.stlport.org port of SGI STL), so that the
wrapper class implements reference counting.

I am puzzled on how a reference counted string should handle capacity()
and reserve().  Does the standard allow the capacity to decrease when
the assignment operator (and similar functions) are applied?

For example:

string a("string a");
a.reserve(1024);

string b();
a = b;

If reference counting is used, a will now have b's capacity.


{this was posted last week to comp.lang.moderated.c++, but no replies
were posted}
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]