Question about iterator management in sw/source/core/fields/cellfml.cxx

Terrence Enger tenger at iseries-guru.com
Fri Feb 3 09:17:33 PST 2012


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."




More information about the LibreOffice mailing list