http://gcc.gnu.org/bugzilla/show_bug.cgi?id=50119
Bug #: 50119 Summary: copy_n advances InputIterator one more time than necessary Classification: Unclassified Product: gcc Version: 4.7.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: libstdc++ AssignedTo: unassig...@gcc.gnu.org ReportedBy: cu...@cubbi.org The naive implementation of std::copy_n() incorrectly increments InputIterator one more time than needed, which fails with single-pass input iterators such as istream_iterator Test program: #include <algorithm> #include <vector> #include <sstream> #include <iterator> #include <iostream> int main() { std::istringstream s("1 2 3 4 5"); std::vector<int> v; copy_n(std::istream_iterator<int>(s), 2, back_inserter(v)); copy_n(std::istream_iterator<int>(s), 2, back_inserter(v)); copy(v.begin(), v.end(), std::ostream_iterator<int>(std::cout, " ")); std::cout << '\n'; } Output with GCC 4.5.3 and 4.7.0-alpha20110716 1 2 4 5 expected output: 1 2 3 4 Similar bug was fixed in LLVM's libc++ in february 2011, the fix is straightforward: http://lists.cs.uiuc.edu/pipermail/cfe-commits/Week-of-Mon-20110221/039404.html