Re: Sequence container capacity after calling clear()
On Sunday, March 24, 2013 3:30:10 PM UTC, Leigh Johnston wrote:
On 24/03/2013 15:04, James Kanze wrote:
On Saturday, March 23, 2013 9:30:02 PM UTC, Leigh Johnston wrote:
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().
Has the specification in C++11 changed? In C++03, such an
implimentation would be illegal.
As I said in my post it is unspecified what happens to a vector's
capacity when calling clear().
Not in C++03, at least. It may be overspecification, but
clear() is defined to be exactly the same a erase(begin(),
end()). And that doesn't allow reducing capacity.
Could you tell me which implementation you are talking about.
I've not verified recently, but neither g++ nor VC++ behaved
like this in the past.
QNX.
I'm not familiar with it, but if clear() changes the capacity,
I'd send in a bug report.
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.
There is nothing that I can see (in C++03) which allows an
implementation to reduce the value of container.capacity() when
calling clear.
I repeat: the behaviour was and is unspecified.
You can repeat it as much as you like. It's your word against
the standard, and in such cases, the standard rules. A quick
check does seem to indicate that C++11 has changed the
definition of clear(). The new definition is fairly ambiguous,
but I still don't see where it authorizes the capacity to
change.
--
James