A simple summation loop is slow. The reason: it++ in the loop. Postfix returns the old iterator value, so it copies the iterator on every step — and this iterator carries a 512-byte buffer in each copy. Nobody even uses the value it++ returns.
The loop uses it++. Postfix increment always makes a copy of the iterator — it has to return the old value. This iterator is expensive to copy. Change the loop so it makes zero copies; the test checks the copy counter.
HeavyIterator itself stays unchanged — fix only the traversaloperator++(int) return the previous value, and why does that force a copy?it++ is not used, what may the compiler still NOT optimize away, and why?Postfix it++ must hand back the value from before the step. Where can it take that value from, if not a copy?
When nobody reads the result of the increment, the form is your choice — and one form never copies.
Hit Submit (or ⌘/Ctrl + ↵) — test results will show up here.