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

Subject: Re: [Boost-bugs] [Boost C++ Libraries] #5825: constructing function_input_iterator without consuming an item
From: Boost C++ Libraries (noreply_at_[hidden])
Date: 2012-01-03 11:32:01


#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
Resolution: | Keywords: function_input_iterator past-the-end
------------------------------------------------------------------------+---

Comment (by charlie@…):

 I have the same problem - here's a simple example where we try to emulate
 a boost counting iterator. What's particularly nasty is that the two calls
 to make_function_input_iterator are called in arbitrary order, so the
 behaviour isn't even defined, as either of the two iterators could end up
 with the first element.

 {{{
 #include <boost/iterator/function_input_iterator.hpp>
 #include <boost/range/algorithm.hpp>
 #include <iostream>
 #include <vector>

 class Counter
 {
 public:
   typedef int result_type;
   Counter(int initial)
   : m_val(initial)
   { }

   int operator()()
   {
     std::cout << "Returning: " << m_val << std::endl;
     return m_val++;
   }

 private:
   int m_val;
 };

 int main()
 {
   Counter c(10);
   std::vector<int> v;
   std::copy(boost::make_function_input_iterator(c, 0),
             boost::make_function_input_iterator(c, 5),
             std::back_inserter(v));
   boost::copy(v, std::ostream_iterator<int>(std::cout, " "));
   std::cout << std::endl;

   return 0;
 }
 }}}

 Here's the output:
 {{{
 Returning: 10
 Returning: 11
 Returning: 12
 Returning: 13
 Returning: 14
 Returning: 15
 Returning: 16
 11 12 13 14 15
 }}}

 I believe the iterators should be modified to never invoke the generator
 initially until they are dereferenced (so in particular the second
 iterator should never invoke the generator if used as an end iterator).

-- 
Ticket URL: <https://svn.boost.org/trac/boost/ticket/5825#comment:1>
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:08 UTC