J
James Kanze
for ( std::size_t i = myLength; i--; )
{
//...
}
Nice obfuscation.
for ( std::size_t i = myLength; i--; )
{
//...
}
I was using int instead of std::size_t becuase loops like
this, which are used in several routines, do not work:
---
std::size_t myLength(myVector.size());
for(std::size_t i(myLength-1);i>=0;i--)
{
... do something ...
}
---
"i" decreases down beyond zero. But with an int, ie.
---
int myLength(myVector.size());
for(int i(myLength-1);i>=0;i--)
{
... do something ...
}
---
it works fine.
Does that make any sense? It seems you've just got to have
a conversion somewhere.
If you want the range myLenght-1 downto 0, inclusive, consider just
for( size_t i = myLength-1; i != size_t(-1); --i )
Cheers, & hth.,
I have to say that this example is too clever for my taste. I'd rather
write:
for (size_t i = myLength; i != 0; ) {
--i;
// whatever
}
One reason for my preference is that this technique works for bidirectional
iterators too:
for (list<T>::iterator it = myList.end(); it != myList.begin(); ) {
--it;
// whatever
}
whereas the "myLength-1" technique doesn't.
Nice obfuscation.
In message
That's another word for "idiom", isn't it? Guaranteed
termination, loop invariant exists throughout the block, ...
what more do you want?
* Andrew Koenig:
I have to say that that example is too clever for /my/ taste... ;-)
With a for-loop, except "for(;", one expects the expression
that in some sense "counts" (or less informally speaking,
controls the loop variant), to be in the loop head, after that
last semicolon.
Also, one expects the initializer to be the first value for
the loop control variable.
Hm. I don't think it's necessarily a good idea to press
different kinds of code into the same form, or using the
possibility of doing so as a positive guideline. And I think
the above would be better expressed using reverse iterators,
placing the update in the for loop head, like
typedef list<T>::reverse_iterator RIterator;
for( RIterator it = myList.rbegin(); it != myList.rend(); ++it )
{
// Whatever
}
If it's an idiom, you don't need to read it. Whether it's an idiom isReadability.
James said:Nice obfuscation.
I was happy with this until yesterday, when a colleague pointed out thatRalph D. Ungermann said:The postfix `trick' is uncommon, but not unknown. After a first
irritation, one ought to get it -- and remember, if repeated.
Though the decrement is more correctly placed at the beginning of the
loop, this does neither enhance readability nor safety (IMHO).
I've seen too much code, where each loop has been handcrafted: mixing
int and size_t, cast both ways, decrementing i on-the-fly at the first
occurrance in the loop body, and worse. You'll agree, that this causes
much more obfuscation.
So I use my way, whenever I have to: using uncommon syntax for uncommon
loops.
But as you mention it, I'm willing to change my style to:
for ( std::size_t i = myLength; i--; /*backward*/ )
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.