Date: prev next · Thread: first prev next last
2012 Archives by date, by thread · List index


On Fri, 2012-02-03 at 17:31 +0100, Michael Stahl wrote:
On 03/02/12 17:21, Stephan Bergmann wrote:
On 02/03/2012 04:18 PM, Michael Stahl wrote:
On 03/02/12 14:01, Stephan Bergmann wrote:
The "standard idiom" is

    for (iterator i = m.begin(); i != m.end();) {
      if (doErase) {
        m.erase(i++);
      } else {
        ++i;
      }
    }

but doesn't that have the same problem? "i" is incremented only after
the erase is complete, when "i" is already invalid.

No, i is incremented before calling erase in the above code.

ah, that's surprising.
see, that is why i almost always write the i++ as an extra statement,
i'm never quite exactly sure what it does, and when  :)

Stephan, I am sorry to question your expertise, but I wonder ... is
your reassurance based on knowledge of the language standard, or is
based on observed behaviour of C++ compilers?

It is guaranteed that i will be incremented after it is copied (I am
*assuming* call-by-value.) for use by erase().  I suspect (but do not
know) that the question of whether the increment happens before or
after the call to erase() may be implementation-defined or worse.

(This, of course, is independent of what STL says about the validity
of iterators after erase().)

Terry, who says
    "This is your last warning.  I have a semicolon, and I am not
    afraid to use it."



Context


Privacy Policy | Impressum (Legal Info) | Copyright information: Unless otherwise specified, all text and images on this website are licensed under the Creative Commons Attribution-Share Alike 3.0 License. This does not include the source code of LibreOffice, which is licensed under the Mozilla Public License (MPLv2). "LibreOffice" and "The Document Foundation" are registered trademarks of their corresponding registered owners or are in actual use as trademarks in one or more countries. Their respective logos and icons are also subject to international copyright laws. Use thereof is explained in our trademark policy.