Re: ++i++ for iterator in loops (gcc-3.4.4)
On Feb 6, 1:55 pm, Ron Natalie <r...@spamcop.net> wrote:
Frank Bergemann wrote:
i stumbled this in source code review:
VALID(?!):
It's undefined behavior for integers.
It requires a diagnostic for integers (and other non-class
types). The only time it doesn't require a diagnostic is if the
iterator is a class type, and prefix ++ is a member function.
It's possibly undefined behavior for iterators, but
certainly something that you can't rely on being well
defined.
It's always undefined behavior for iterators---the standard (at
least the latest draft, but I don't think that there've been any
changes here) only requires ++ (prefix or postfix) to work on an
lvalue. And any time you violate the constraints of a standard
function, you have undefined behavior. (Which, of course, may
appear to work.
What is it supposed to do anyhow.
++i, ++i
would be well defined and clearer in intent.
That's not what it does. I rather suspect that it is just a
typo. The original code was probably just "i++", someone went
on a misguided attempt to convert all of the postfix to prefix,
and after adding the prefix, forgot to delete the postfix.
If this is the case, it does provide two interesting lessons in
softare engineering:
-- Requiring all incrementation to be prefix isn't a good idea
if you already have code where it is postfix, regardless of
what some people clain.
-- It's a good example of an error which slips through testing,
and of why good code review is necessary.
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34