Fix non-standard behaviour of std::istream_iterator
authorJonathan Wakely <jwakely@redhat.com>
Wed, 19 Jun 2019 22:57:10 +0000 (23:57 +0100)
committerJonathan Wakely <redi@gcc.gnu.org>
Wed, 19 Jun 2019 22:57:10 +0000 (23:57 +0100)
commit638ad333ec544ed7873ed6e13b6f095d6d58fd3b
treecc296691181aaf4a131f07a799482aba89f10863
parent36d0dada6773d7fd7c5ace64c90e723930a3b81e
Fix non-standard behaviour of std::istream_iterator

The current implementation of istream_iterator allows the iterator to be
reused after reaching end-of-stream, so that subsequent reads from the
stream can succeed (e.g. if the stream state has been cleared and stream
position changed from EOF). The P0738R2 paper clarified that the
expected behaviour is to set the stream pointer to null after reaching
end-of-stream, preventing further reads.

This implements that requirement, and adds the new default constructor
to std::ostream_iterator.

* include/bits/stream_iterator.h (istream_iterator::_M_equal()): Make
private.
(istream_iterator::_M_read()): Do not check stream state before
attempting extraction. Set stream pointer to null when extraction
fails (P0738R2).
(operator==(const istream_iterator&, const istream_iterator&)): Change
to be a hidden friend of istream_iterator.
(operator!=(const istream_iterator&, const istream_iterator&)):
Likewise.
(ostream_iterator::ostream_iterator()): Add default constructor.
(ostream_iterator::ostream_iterator(ostream_type*, const C*)): Use
addressof.
* testsuite/24_iterators/istream_iterator/1.cc: New test.
* testsuite/24_iterators/ostream_iterator/1.cc: New test.
* testsuite/24_iterators/ostream_iterator/70766.cc: Also check
constructor taking a string.
* testsuite/24_iterators/ostream_iterator/requirements/constexpr.cc:
New test.

From-SVN: r272491
libstdc++-v3/ChangeLog
libstdc++-v3/include/bits/stream_iterator.h
libstdc++-v3/testsuite/24_iterators/istream_iterator/1.cc [new file with mode: 0644]
libstdc++-v3/testsuite/24_iterators/ostream_iterator/1.cc [new file with mode: 0644]
libstdc++-v3/testsuite/24_iterators/ostream_iterator/70766.cc
libstdc++-v3/testsuite/24_iterators/ostream_iterator/requirements/constexpr.cc [new file with mode: 0644]