This is the mail archive of the
libstdc++@gcc.gnu.org
mailing list for the libstdc++ project.
Re: Optimzed vs. readable code
On Wed, Nov 30, 2005 at 06:14:44PM +0100, Pierre THIERRY wrote:
> I don't see the point. I repeat: i would not have even proposed the
> modification if I wasn't expecting any decent compiler to do the
> following optimization:
>
> void foo(Iterator first, Iterator last)
> {
> Iterator current = first;
> for ( ; current != last; ++current)
> do_something(current);
> }
> }
>
> becomes:
>
> void foo(Iterator first, Iterator last)
> {
> for ( ; first != last; ++first)
> do_something(first);
> }
> }
Then gcc is not a decent compiler. Actually, I suspect that no conforming
C++ compiler is allowed to be "decent" in the general case: if the
constructor or destructor of Iterator has a side effect, the optimization
you suggest would change the order of side effects. There are cases
where this is allowed (elimination of copy constructors), but I don't
think that this is such a case.