Usually post-increment and pre-increment differ in only two respects: they return a different value, and post-increment is slightly more expensive because it requires making a copy of the variable. The return value is not used in a for
loop, so if we want post- to be better than pre-, we must invent a new Container
class whose Iterator
has a weird and costly pre-increment operator. Something like
operator++()
{
ptr = ptr->next;
// perform some undefined behavior, or just hash the Beijing telephone book
return *this;
}
This can be done as a result of simple incompetence. As for a real reason to put something bad in operator++()
, I'm stumped.
P.S. I had a subordinate once who insisted that post-increment was correct, and that using pre-increment in the for
loop would give different (wrong) results. Repeatedly correcting him didn't help; the fact that he could have tested this hypothesis very easily made no difference. He had somehow worked as a software engineer for over ten years without getting good at it.