On Sunday, 24 March 2013 05:05:32 UTC, Daniel Krügler wrote:
> Am 23.03.2013 23:27, schrieb Leigh Johnston:
> > 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.
> This problem was already cause of a previous library issue:
>
http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-closed.html#1102
> Please provide this link to the vendor of the buggy library you have in mind.
I, for one, would like to see the issue reopened. I understand
the logic behind the current resolution, but I think it is less
than clear. Particularly because:
1. The guarantees with regards to the validity of iterators and
the absense of reallocation are given with regards to
a previous call to reserve. In other words, clear() cannot
reduce the capacity IF the capacity was the result of a call
to reserve. Now, we all know that no implementation is
going to make clear() behave differently depending on
whether reserve() was called or not, but technically, it
might be legal.
2. C++11 has changed the definition of clear(). It used to be
defined in terms of erase(); erase() is documented to only
invalidate iterators at or after the elements which are
erased. Logically, I suppose that one could argue that if
all of the elements are erased, it could invalidate all
iterators, but again, one would not expect such special
casing, and it would seem to violate at least the intent of
the standard. Now that clear() is no longer defined in
terms of erase(), however, such indirect argumentation is
seriously weakened.
I'd really like to see this specified a bit clearer, even if
there seems to be a consensus in the committee as to what is
meant. (I noticed that C++11 also reduces guarantees elsewhere,
and has rendered an expression like s[i] == s[j] invalid if s is
a non-const std::string. Luckily, all of the implementors seem
to know what is wanted, and implement something reasonable, but
still, it would be nice to have it guaranteed.)
--
James