Review Request: some krazy (and cppcheck) cleanups

Esben Mose Hansen kde at
Sun Dec 6 14:34:57 GMT 2009

On Saturday 05 December 2009 23:53:55 Andrew Coles wrote:
> Whilst you're changing loops based on iterators, it's better to cache the
>  value of foo.end() in a const variable before the start of the loop:
> {
>   std::list<int>::iterator itr = numbers.begin();
>   const std::list<int>::iterator itrEnd = numbers.end();
>   for (; itr != itrEnd; ++itr) {
>   ...
>   }
> }
> (Note the extra braces - that way the scope of the iterator variables is
>  still limited to just the for loop.)

You can cut down a lot on braces and stuff by doing this instead:

for (std::list<int>::iterator itr=number.begin(), itrEnd=numbers.end(); 
itr!=itrEnd;++itr) {

Note the comma. Just a trick I learned a lot time ago. 

Also note that in the "trivial" cases, BOOST_FOREACH makes it somewhat more 

Qt's foreach  unfortunately, insists on copying the container (I think), 
making it next-to-unusable except for the implicitly-shared containers, and 
breaking a lot of other useful stuff.

Kind regards, Esben

More information about the kde-core-devel mailing list