Review Request: some krazy (and cppcheck) cleanups

Ingo Klöcker kloecker at kde.org
Sun Dec 6 10:37:50 GMT 2009


On Sunday 06 December 2009, Milian Wolff wrote:
> 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) {
> >   ...
> >   }
> > }
>
> Is this really true? I always thought such optimization is not
> required in compiled languages like Cpp because the compiler does it
> for you (esp. for const iterators). If not, I might have to optimize
> some loops I wrote or at least will write future loops like that.

There's no guarantee that the compiler does it for you, so you should 
always do it. It's even better to use Qt's foreach, boost's foreach or 
std::for_each whenever possible because all of them do the right thing 
so that you don't have to think about it.


Regards,
Ingo
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 197 bytes
Desc: This is a digitally signed message part.
URL: <http://mail.kde.org/pipermail/kde-core-devel/attachments/20091206/f44df147/attachment.sig>


More information about the kde-core-devel mailing list