[Boost-bugs] [Boost C++ Libraries] #5825: constructing function_input_iterator without consuming an item

Subject: [Boost-bugs] [Boost C++ Libraries] #5825: constructing function_input_iterator without consuming an item
From: Boost C++ Libraries (noreply_at_[hidden])
Date: 2011-08-29 20:24:39


#5825: constructing function_input_iterator without consuming an item
-----------------------------------------------------------------------+----
 Reporter: Maximiliano Garrone ten Brink <maximiliano@…> | Owner: dave
     Type: Bugs | Status: new
Milestone: To Be Determined | Component: iterator
  Version: Boost 1.47.0 | Severity: Problem
 Keywords: function_input_iterator past-the-end |
-----------------------------------------------------------------------+----
 The only constructor for function_input_iterator always consumes an item
 from the generator. This makes it difficult to define a past-the-end
 iterator.

 The first example given in
 http://www.boost.org/doc/libs/1_47_0/libs/iterator/doc/function_input_iterator.html
 calls rand() 11 times instead of the 10 expected. This can be a more
 serious problem when, for example, reading a file, as shown in the
 following program:


 {{{
 #include <string>
 #include <fstream>
 #include <iostream>
 #include <algorithm>

 #include <boost/iterator/function_input_iterator.hpp>

 static const std::string filename( "test.txt" );

 struct generator {
     typedef int result_type;
     generator() : in( filename ) {}

     result_type operator() () {
                 result_type ret;
                 in >> ret;
                 return ret;
     }

         std::ifstream in;
 };

 int main(int argc, char * argv[]) {
         std::ofstream out( filename );
         out << 0 << std::endl
                 << 1 << std::endl
                 << 2 << std::endl
                 << 3 << std::endl;

     generator f;
     std::copy(
             boost::make_function_input_iterator(f, 0),
             boost::make_function_input_iterator(f, 3),
             std::ostream_iterator<int>(std::cout, " ")
         ); // "1 2 3" is shown on cout, instead of "0 1 2"
     return 0;
 }
 }}}

 A possible solution could be adding a different constructor to
 function_input_iterator specifically to represent a past-the-end iterator.

-- 
Ticket URL: <https://svn.boost.org/trac/boost/ticket/5825>
Boost C++ Libraries <http://www.boost.org/>
Boost provides free peer-reviewed portable C++ source libraries.

This archive was generated by hypermail 2.1.7 : 2017-02-16 18:50:07 UTC