http://gcc.gnu.org/bugzilla/show_bug.cgi?id=51823
Bug #: 51823 Summary: reverse iterator instantiated with POD type returns uninitialized values + work around Classification: Unclassified Product: gcc Version: 4.6.1 Status: UNCONFIRMED Severity: normal Priority: P3 Component: libstdc++ AssignedTo: unassig...@gcc.gnu.org ReportedBy: jakobsyb...@gmail.com Created attachment 26299 --> http://gcc.gnu.org/bugzilla/attachment.cgi?id=26299 *.i* output of minimal example that reproduces the bug System info: $ uname -a Linux ****** 3.0.0-14-generic #23-Ubuntu SMP Mon Nov 21 20:28:43 UTC 2011 x86_64 x86_64 x86_64 GNU/Linux $ g++ -v Using built-in specs. COLLECT_GCC=gcc COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/4.6.1/lto-wrapper Target: x86_64-linux-gnu Configured with: ../src/configure -v --with-pkgversion='Ubuntu/Linaro 4.6.1-9ubuntu3' --with-bugurl=file:///usr/share/doc/gcc-4.6/README.Bugs --enable-languages=c,c++,fortran,objc,obj-c++,go --prefix=/usr --program-suffix=-4.6 --enable-shared --enable-linker-build-id --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.6 --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --enable-plugin --enable-objc-gc --disable-werror --with-arch-32=i686 --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu Thread model: posix gcc version 4.6.1 (Ubuntu/Linaro 4.6.1-9ubuntu3) Attachment: * ostream_iterator_reverse_iterator.ii: output of: g++ -save-temps -o ostream_iterator_reverse_iterator -Wall ostream_iterator_reverse_iterator.cc where ostream_iterator_reverse_iterator.cc is a minimal example that reproduces the bug. Headerfiles involved: <iterator> Compile command: g++ -o ostream_iterator_reverse_iterator -Wall ostream_iterator_reverse_iterator.cc Description: Consider an iterator class that implements the requirements of a forward_iterator. We underlying datatype is always POD and the operator*() member of the iterator returns the results by value, since it is POD anyway. Upon creating a reverse_iterator from this class a simple for-loop produces the expected results: Returning 9 9 Returning 8 8 Returning 7 7 Returning 6 6 Returning 5 5 Returning 4 4 Returning 3 3 Returning 2 2 Returning 1 1 Returning 0 0 but using the reverse_iterator in std::copy() with std::ostream_iterator produces: Returning 9 32767 Returning 8 32767 Returning 7 32767 Returning 6 32767 Returning 5 32767 Returning 4 32767 Returning 3 32767 Returning 2 32767 Returning 1 32767 Returning 0 32767 It seems that the internal value does not get copied. When using -O3 both the for-loop and the copy() construct produce 0 (zero) for each call to operator*() in the reverse_iterator, but not in the normal iterator. The 'work-around' forces the use of forwarding. I put it as comment in the minimum example. If I should have attached a separate source file with just the operator*()-line replaced, I wish to express my apologies in advance. The source file with the work-around instead of the original version of operator*() needs an additional compiler option though: -std=c++0x However, even with this version, the result with -O3 is still all zeros; not what I expected, but since that is the same as before, I guess that is more likely to be me misunderstanding what is supposed to happen with -O3 switched on.