Re: static_cast vs reinterpert_cast
In message <xQrtg.11410$j7.315516@news.indigo.ie>, Frederick Gotham
<fgothamNO@SPAM.com> writes
Richard Herring posted:
for(size_t i = len - 1; size_t(-1) != i; --i)
I'd lose the -1 altogether ;-)
for (size_t i = len; i-- != 0; )
Yes but this has re-arranged the natural order of the loop.
I like my "for" loops to have four phases:
(1) The "initialisation stuff" is executed first of all.
(2) The condition is tested.
(3) The body is executed.
(4) The "prepare for next iteration" stuff is executed.
Your example mixes (2) and (4), which I don't like.
It puts (4) before (3), that's all. It's just another idiom, and a very
clear one once you're used to it. The only numbers that appear in it are
0 and len, exactly the same as for the forward loop. Other solutions all
require an off-by-one representation of the index and either mix signed
and unsigned, as with the cast above, or do something even more
contorted.
--
Richard Herring