Boost C++ Libraries: Ticket #8617: Surprising/wrong semantics of counting_range https://svn.boost.org/trac10/ticket/8617 <p> I would expect </p> <p> counting_range(begin(rng),end(rng)); </p> <p> to be equivalent to </p> <p> counting_range(rng); </p> <p> which does not seem to be the case. The code in the library looks like it constructs the range from the first and last _value_ of the rng: </p> <blockquote> <p> template&lt;class Range&gt; </p> <blockquote> <p> inline iterator_range&lt;counting_iterator&lt;BOOST_DEDUCED_TYPENAME range_value&lt;Range&gt;::type&gt; &gt; counting_range(Range&amp; rng) { </p> <blockquote> <p> typedef counting_iterator&lt;BOOST_DEDUCED_TYPENAME range_value&lt;Range&gt;::type&gt; counting_iterator_t; typedef iterator_range&lt;counting_iterator_t&gt; result_t; return boost::empty(rng) </p> <blockquote> <p> ? result_t() : result_t( </p> <blockquote> <p> counting_iterator_t(*boost::begin(rng)), counting_iterator_t(*boost::prior(boost::end(rng)))); </p> </blockquote> </blockquote> </blockquote> <p> } </p> </blockquote> </blockquote> <p> I want to be able to use the counting range in a range based for like this: </p> <p> for(auto it : counting_range(rng | indexed(0))) { *it; it.index(); } I also vote for a way to use indexed adaptor in the range based for loop directly. The model for the new indexed adaptor could be something like enumerate() in Python. Much much nicer than using a counter variable. </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/8617 Trac 1.4.3 Nathan Ridge Thu, 30 May 2013 22:41:42 GMT <link>https://svn.boost.org/trac10/ticket/8617#comment:1 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/8617#comment:1</guid> <description> <p> I confess I find it surprising that an overload of counting_range() that takes a range exists at all. </p> <p> I agree that constructing a counting range from the first and last values of a range seems arbitrary, but constructing a counting range from the begin and end *iterator* of a range seems pointless (how would the resulting range be different from the original one?). </p> <p> Neil, what use cases did you have in mind for this overload of counting_range()? </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Neil Groves</dc:creator> <pubDate>Fri, 21 Feb 2014 20:37:27 GMT</pubDate> <title>status changed https://svn.boost.org/trac10/ticket/8617#comment:2 https://svn.boost.org/trac10/ticket/8617#comment:2 <ul> <li><strong>status</strong> <span class="trac-field-old">new</span> → <span class="trac-field-new">assigned</span> </li> </ul> <p> I believe this is just very wrong. I'll sort this out. I'm inclined to remove counting_range to avoid a surprising change of semantic and replace it with something that is less bonkers. </p> Ticket marc.glisse@… Fri, 21 Feb 2014 23:25:09 GMT <link>https://svn.boost.org/trac10/ticket/8617#comment:3 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/8617#comment:3</guid> <description> <p> Replying to <a class="ticket" href="https://svn.boost.org/trac10/ticket/8617#comment:1" title="Comment 1">nathanridge</a>: </p> <blockquote class="citation"> <p> I confess I find it surprising that an overload of counting_range() that takes a range exists at all. </p> <p> I agree that constructing a counting range from the first and last values of a range seems arbitrary, but constructing a counting range from the begin and end *iterator* of a range seems pointless (how would the resulting range be different from the original one?). </p> </blockquote> <p> It should be the reciprocal of <code>adaptors::indirect</code>. In a range for loop, there is an implicit iterator <code>iter</code> going from begin to end, and you get <code>*iter</code> for each element. With indirect, you get <code>**iter</code>. With counting_range (constructed from iterators, because the constructor from a range is too weird), you get <code>iter</code> (not dereferenced). It can be extremely useful and should be available in boost adaptors with the usual <code>|</code> syntax and a clearer name. This is a documented use of <code>counting_iterator</code>. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Neil Groves</dc:creator> <pubDate>Thu, 27 Feb 2014 17:41:23 GMT</pubDate> <title>status changed; resolution set https://svn.boost.org/trac10/ticket/8617#comment:4 https://svn.boost.org/trac10/ticket/8617#comment:4 <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> <p> The counting_range is now just a dumb wrapper for a range of counting_iterator with entirely consistent semantics to the underlying iterator. I agree that other counting type ranges and adapters are useful but that's not this ticket. This ticket is about the defect of the counting_range function that takes ranges. </p> <p> The adaptor that has been mentioned may be useful, but I don't want to make it a counted adaptor. This is just too bonkers even for my code! I am happy to create something else for doing the opposite of indirecting (directing?!). I shall consider adding a directed adaptor. </p> Ticket Nathan Ridge Thu, 27 Feb 2014 17:52:56 GMT <link>https://svn.boost.org/trac10/ticket/8617#comment:5 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/8617#comment:5</guid> <description> <p> Replying to <a class="ticket" href="https://svn.boost.org/trac10/ticket/8617#comment:4" title="Comment 4">neilgroves</a>: </p> <blockquote class="citation"> <p> I am happy to create something else for doing the opposite of indirecting (directing?!). I shall consider adding a directed adaptor. </p> </blockquote> <p> In P-Stade Oven it is called 'outdirected': <a class="ext-link" href="http://p-stade.sourceforge.net/oven/doc/html/oven/range_adaptors.html#oven.range_adaptors.outdirected"><span class="icon">​</span>http://p-stade.sourceforge.net/oven/doc/html/oven/range_adaptors.html#oven.range_adaptors.outdirected</a>. (I'm not necessarily saying we should call it the same, just pointing out the "prior art".) </p> </description> <category>Ticket</category> </item> </channel> </rss>