Boost C++ Libraries: Ticket #5825: constructing function_input_iterator without consuming an item https://svn.boost.org/trac10/ticket/5825 <p> 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. </p> <p> The first example given in <a href="http://www.boost.org/doc/libs/1_47_0/libs/iterator/doc/function_input_iterator.html">http://www.boost.org/doc/libs/1_47_0/libs/iterator/doc/function_input_iterator.html</a> 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: </p> <pre class="wiki">#include &lt;string&gt; #include &lt;fstream&gt; #include &lt;iostream&gt; #include &lt;algorithm&gt; #include &lt;boost/iterator/function_input_iterator.hpp&gt; static const std::string filename( "test.txt" ); struct generator { typedef int result_type; generator() : in( filename ) {} result_type operator() () { result_type ret; in &gt;&gt; ret; return ret; } std::ifstream in; }; int main(int argc, char * argv[]) { std::ofstream out( filename ); out &lt;&lt; 0 &lt;&lt; std::endl &lt;&lt; 1 &lt;&lt; std::endl &lt;&lt; 2 &lt;&lt; std::endl &lt;&lt; 3 &lt;&lt; std::endl; generator f; std::copy( boost::make_function_input_iterator(f, 0), boost::make_function_input_iterator(f, 3), std::ostream_iterator&lt;int&gt;(std::cout, " ") ); // "1 2 3" is shown on cout, instead of "0 1 2" return 0; } </pre><p> A possible solution could be adding a different constructor to function_input_iterator specifically to represent a past-the-end iterator. </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/5825 Trac 1.4.3 charlie@… Tue, 03 Jan 2012 11:32:01 GMT <link>https://svn.boost.org/trac10/ticket/5825#comment:1 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/5825#comment:1</guid> <description> <p> 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. </p> <pre class="wiki">#include &lt;boost/iterator/function_input_iterator.hpp&gt; #include &lt;boost/range/algorithm.hpp&gt; #include &lt;iostream&gt; #include &lt;vector&gt; class Counter { public: typedef int result_type; Counter(int initial) : m_val(initial) { } int operator()() { std::cout &lt;&lt; "Returning: " &lt;&lt; m_val &lt;&lt; std::endl; return m_val++; } private: int m_val; }; int main() { Counter c(10); std::vector&lt;int&gt; 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&lt;int&gt;(std::cout, " ")); std::cout &lt;&lt; std::endl; return 0; } </pre><p> Here's the output: </p> <pre class="wiki">Returning: 10 Returning: 11 Returning: 12 Returning: 13 Returning: 14 Returning: 15 Returning: 16 11 12 13 14 15 </pre><p> 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). </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Dave Abrahams</dc:creator> <pubDate>Tue, 03 Jan 2012 11:51:10 GMT</pubDate> <title>severity changed https://svn.boost.org/trac10/ticket/5825#comment:2 https://svn.boost.org/trac10/ticket/5825#comment:2 <ul> <li><strong>severity</strong> <span class="trac-field-old">Problem</span> → <span class="trac-field-new">Showstopper</span> </li> </ul> <p> I agree; this looks like a serious problem. </p> Ticket Dean Michael Berris Mon, 27 Aug 2012 11:39:33 GMT owner changed; cc set https://svn.boost.org/trac10/ticket/5825#comment:3 https://svn.boost.org/trac10/ticket/5825#comment:3 <ul> <li><strong>cc</strong> <span class="trac-author">mikhailberis@…</span> added </li> <li><strong>owner</strong> changed from <span class="trac-author">Dave Abrahams</span> to <span class="trac-author">Dean Michael Berris</span> </li> </ul> <p> I wasn't watching this and I've just gotten time to take a look at this now. Let me whip up a patch to fix this. </p> Ticket Dean Michael Berris Mon, 27 Aug 2012 12:30:09 GMT <link>https://svn.boost.org/trac10/ticket/5825#comment:4 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/5825#comment:4</guid> <description> <p> Update: I have a patch ready locally, just getting the necessary approvals to release the code under the Boost Software License. If things go well I should have something late Pacific Time, Monday August 27. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Dean Michael Berris</dc:creator> <pubDate>Mon, 27 Aug 2012 23:53:42 GMT</pubDate> <title>attachment set https://svn.boost.org/trac10/ticket/5825 https://svn.boost.org/trac10/ticket/5825 <ul> <li><strong>attachment</strong> → <span class="trac-field-new">boost-5825.patch</span> </li> </ul> <p> Fix to implementation and docs. </p> Ticket Dean Michael Berris Mon, 27 Aug 2012 23:54:18 GMT <link>https://svn.boost.org/trac10/ticket/5825#comment:5 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/5825#comment:5</guid> <description> <p> The attached patch applies to Boost trunk. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Dean Michael Berris</dc:creator> <pubDate>Mon, 27 Aug 2012 23:55:09 GMT</pubDate> <title>status changed https://svn.boost.org/trac10/ticket/5825#comment:6 https://svn.boost.org/trac10/ticket/5825#comment:6 <ul> <li><strong>status</strong> <span class="trac-field-old">new</span> → <span class="trac-field-new">assigned</span> </li> </ul> Ticket Dean Michael Berris Mon, 27 Aug 2012 23:55:50 GMT status changed; resolution set https://svn.boost.org/trac10/ticket/5825#comment:7 https://svn.boost.org/trac10/ticket/5825#comment:7 <ul> <li><strong>status</strong> <span class="trac-field-old">assigned</span> → <span class="trac-field-new">closed</span> </li> <li><strong>resolution</strong> → <span class="trac-field-new">fixed</span> </li> </ul> Ticket jeffrey.hellrung Sun, 09 Sep 2012 15:33:16 GMT <link>https://svn.boost.org/trac10/ticket/5825#comment:8 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/5825#comment:8</guid> <description> <p> (In <a class="changeset" href="https://svn.boost.org/trac10/changeset/80467" title="fix #5825; fix #7194">[80467]</a>) fix <a class="closed ticket" href="https://svn.boost.org/trac10/ticket/5825" title="#5825: Bugs: constructing function_input_iterator without consuming an item (closed: fixed)">#5825</a>; fix <a class="closed ticket" href="https://svn.boost.org/trac10/ticket/7194" title="#7194: Bugs: Error in documentation of make_function_input_iterator (closed: fixed)">#7194</a> </p> </description> <category>Ticket</category> </item> </channel> </rss>