Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.std.c++ > #605

Re: Sequence container capacity after calling clear()

From Jason McKesson <jmckesson@googlemail.com>
Newsgroups comp.std.c++
Subject Re: Sequence container capacity after calling clear()
Date 2013-03-23 22:05 -0700
Organization unknown
Message-ID <ddcc5fe4-9834-4f95-84a7-8e297e6c6cee@googlegroups.com> (permalink)
References <dZydnUi3X6YIOtHMnZ2dnUVZ8iCdnZ2d@giganews.com>

Show all headers | View raw


On Saturday, March 23, 2013 2:30:03 PM UTC-7, Leigh Johnston wrote:
> Hi,
>
>
>
> Can we please change the ISO C++ Standard so that explicitly states what
>
> happens to a sequence container's capacity() after calling clear()?
>
>
>
> Currently the behaviour is unspecified and I know of at least one
>
> implementation that deallocates on vector<T>::clear().
>
>
>
> If the behaviour remains unspecified then it is effectively impossible
>
> to write portable code that uses clear() and you have to hope things
>
> such as v.erase(v.begin(), v.end()) behave more consistently across
>
> different implementations.

I don't see what's non-portable about this.

If you don't care what the capacity is, you use `clear` and let the
implementation decide. It's not non-portable because your code
*doesn't care*. If you *do* care about the capacity, then you either
want the capacity to be zeroed out or you want it to be preserved. And
you already have the tools to do either:

    vec.resize(0); //Destroys contents, but doesn't change the capacity.
    vec = vector<T>{}; //Destroys contents and reduces the capacity.

Both of these are reasonable behaviors, so the user should be able to
do either if they want a specific result.


--
[ comp.std.c++ is moderated.  To submit articles, try posting with your ]
[ newsreader.  If that fails, use mailto:std-cpp-submit@vandevoorde.com ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]

Back to comp.std.c++ | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

Sequence container capacity after calling clear() Leigh Johnston <leigh@i42.co.uk> - 2013-03-23 16:27 -0600
  Re: Sequence container capacity after calling clear() Daniel Krügler <daniel.kruegler@googlemail.com> - 2013-03-23 22:05 -0700
    Re: Sequence container capacity after calling clear() James Kanze <james.kanze@googlemail.com> - 2013-03-27 13:28 -0600
  Re: Sequence container capacity after calling clear() Jason McKesson <jmckesson@googlemail.com> - 2013-03-23 22:05 -0700
  Re: Sequence container capacity after calling clear() Juha Nieminen <nospam@thanks.invalid> - 2013-03-24 23:36 -0700
  Re: Sequence container capacity after calling clear() Mathias Gaunard <loufoque@googlemail.com> - 2013-03-25 10:49 -0600
    Re: Sequence container capacity after calling clear() Leigh Johnston <leigh@i42.co.uk> - 2013-03-26 08:55 -0600

csiph-web