http://gcc.gnu.org/bugzilla/show_bug.cgi?id=52427
Bug #: 52427
Summary: problem with default copy constructor and -O
Classification: Unclassified
Product: gcc
Version: 4.6.2
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
AssignedTo: [email protected]
ReportedBy: [email protected]
Created attachment 26777
--> http://gcc.gnu.org/bugzilla/attachment.cgi?id=26777
program with unexpected output
The attached program prints
> 181
> 181
as expected when compiled with "g++-4.6.2 --std=c++0x -o bin/main src/main.cc"
(or g++-4.5.3)
but
> 121
> 181
with any -O option, and even
> 17
> 181
with 4.5.3 with -O.
Replacing the default copy constructor of foo_iterator with a user-defined
constructor that should do the same thing (as in comment in source) fixes the
problem, as does compiling with -fno-elide-constructors. Also, as the testcase
shows, the problem arises with
> sum += *fi++;
but not with
> sum += *fi;
> ++fi;
even though the postfix increment is defined in the usual way; therefore, the
problem seems to lie in the temporary produced by operator++(int).
A GDB session suggested that the optimized-away default copy constructor may be
setting k to 0; that is, k=0 when operator*() is called on the temporary at
'*fi++'; this is consistent with the output. (And moving the "dereference"
inside operator++(int) so that it returns ulong does the same thing.)