Re: Sequence container capacity after calling clear()
On 2013-03-27 03:42, Leigh Johnston wrote:
[..]
I am talking about iterators and references to elements added with,
for example, push_back() *after* calling clear(). I want clear() to
leave the capacity unchanged.
[..]
Consider:
v.reserve(2);
v.push_back(0);
/* do something with v */
v.clear(); // oops, implementation reset capacity to 0
v.push_back(1);
auto i = v.begin();
v.push_back(2);
// i might now be invalid as we may have reallocated
If the Standard made it explicit that clear() leaves a vector's
capacity unchanged then we would not have implementations that
invalidated 'i' above.
Actually I had already responded to this thread, but somehow my reply
did not occur in this group (only on the comp.std.c++ group). The
problem that you are describing was already cause for a previous
library issue:
http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-closed.html#1102
Even NAD issues do have some value to document an existing discussion
of that matter. Personally I agree with you that the wording is less
than clear and should be improved. But for reopening this or for
opening a new issue, there needs to be further evidence that this is
still a problem as of today's compilers. Can you give some pointer for
a library implementation that still changes the capacity during
clear()? I know that previous Visual Studio Compiler libraries did so,
but I thought that this had been fixed in latter version. If you are
aware of newer Library implementations that show these effects that
would be a valuable hint.
There is one newer argument in my mind that could (additionally) be
used to justify a new discussion of that theme: With the acceptance of
http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-defects.html#704
clear() (in the sequence container specification) is no longer defined
in terms of erase(), so one could argue that we have lost further
information about the semantics of that function. This is possibly
only weak evidence, so I would prefer to collect further information
on that topic before reattempting to submit a library issue or to
reopen it.
Feel free to contact my privately in that matter.
HTH & Greetings from Bremen,
Daniel Kr?gler
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]