<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	
	xmlns:georss="http://www.georss.org/georss"
	xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#"
	
	>
<channel>
	<title>
	Comments on: How to Access the Index of the Current Element in a Modern For Loop	</title>
	<atom:link href="https://www.fluentcpp.com/2018/10/26/how-to-access-the-index-of-the-current-element-in-a-modern-for-loop/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.fluentcpp.com/2018/10/26/how-to-access-the-index-of-the-current-element-in-a-modern-for-loop/</link>
	<description>Jonathan Boccara&#039;s blog</description>
	<lastBuildDate>Sun, 02 Dec 2018 13:46:28 +0000</lastBuildDate>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.5.4</generator>
	<item>
		<title>
		By: Pascal B.		</title>
		<link>https://www.fluentcpp.com/2018/10/26/how-to-access-the-index-of-the-current-element-in-a-modern-for-loop/#comment-1513</link>

		<dc:creator><![CDATA[Pascal B.]]></dc:creator>
		<pubDate>Fri, 30 Nov 2018 19:25:00 +0000</pubDate>
		<guid isPermaLink="false">https://www.fluentcpp.com/?p=3918#comment-1513</guid>

					<description><![CDATA[Here&#039;s another implementation using c++17 : http://reedbeta.com/blog/python-like-enumerate-in-cpp17/]]></description>
			<content:encoded><![CDATA[<p>Here&#8217;s another implementation using c++17 : <a href="http://reedbeta.com/blog/python-like-enumerate-in-cpp17/" rel="nofollow ugc">http://reedbeta.com/blog/python-like-enumerate-in-cpp17/</a></p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Miguel Raggi		</title>
		<link>https://www.fluentcpp.com/2018/10/26/how-to-access-the-index-of-the-current-element-in-a-modern-for-loop/#comment-1464</link>

		<dc:creator><![CDATA[Miguel Raggi]]></dc:creator>
		<pubDate>Tue, 13 Nov 2018 16:36:00 +0000</pubDate>
		<guid isPermaLink="false">https://www.fluentcpp.com/?p=3918#comment-1464</guid>

					<description><![CDATA[In reply to &lt;a href=&quot;https://www.fluentcpp.com/2018/10/26/how-to-access-the-index-of-the-current-element-in-a-modern-for-loop/#comment-1463&quot;&gt;jft&lt;/a&gt;.

Well, I rarely have more than 2^31 elements in a container. In any case, just use gsl::index, as I mentioned, which is just a std::ptrdiff_t, which is (usually) a 64-bit int.]]></description>
			<content:encoded><![CDATA[<p>In reply to <a href="https://www.fluentcpp.com/2018/10/26/how-to-access-the-index-of-the-current-element-in-a-modern-for-loop/#comment-1463">jft</a>.</p>
<p>Well, I rarely have more than 2^31 elements in a container. In any case, just use gsl::index, as I mentioned, which is just a std::ptrdiff_t, which is (usually) a 64-bit int.</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: jft		</title>
		<link>https://www.fluentcpp.com/2018/10/26/how-to-access-the-index-of-the-current-element-in-a-modern-for-loop/#comment-1463</link>

		<dc:creator><![CDATA[jft]]></dc:creator>
		<pubDate>Tue, 13 Nov 2018 16:20:00 +0000</pubDate>
		<guid isPermaLink="false">https://www.fluentcpp.com/?p=3918#comment-1463</guid>

					<description><![CDATA[In reply to &lt;a href=&quot;https://www.fluentcpp.com/2018/10/26/how-to-access-the-index-of-the-current-element-in-a-modern-for-loop/#comment-1419&quot;&gt;Miguel Raggi&lt;/a&gt;.

That&#039;s fine if the number of elements in the container is within the positive range of an int. However if there are more elements than this then having i as type size_t is correct. Also it&#039;s probably better to have the index i in its own block such as

&lt;pre&gt;&lt;code&gt;
{
     int i = -1;
        for (auto const&#038; elem : container)
        {
         ++i;
                // do stuff
        }
}
&lt;/code&gt;&lt;/pre&gt;]]></description>
			<content:encoded><![CDATA[<p>In reply to <a href="https://www.fluentcpp.com/2018/10/26/how-to-access-the-index-of-the-current-element-in-a-modern-for-loop/#comment-1419">Miguel Raggi</a>.</p>
<p>That&#8217;s fine if the number of elements in the container is within the positive range of an int. However if there are more elements than this then having i as type size_t is correct. Also it&#8217;s probably better to have the index i in its own block such as</p>
<pre><code>
{
     int i = -1;
        for (auto const&amp; elem : container)
        {
         ++i;
                // do stuff
        }
}
</code></pre>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Alexandr Poltavsky		</title>
		<link>https://www.fluentcpp.com/2018/10/26/how-to-access-the-index-of-the-current-element-in-a-modern-for-loop/#comment-1462</link>

		<dc:creator><![CDATA[Alexandr Poltavsky]]></dc:creator>
		<pubDate>Mon, 12 Nov 2018 20:26:00 +0000</pubDate>
		<guid isPermaLink="false">https://www.fluentcpp.com/?p=3918#comment-1462</guid>

					<description><![CDATA[For vector one could use this:
&#039;&#039;&#039;
    std::vector v;
    for (auto&#038; e : v) 
        auto idx = &#038;e - v.data();
&#039;&#039;&#039;]]></description>
			<content:encoded><![CDATA[<p>For vector one could use this:<br />
&#8221;&#8217;<br />
    std::vector v;<br />
    for (auto&amp; e : v)<br />
        auto idx = &amp;e &#8211; v.data();<br />
&#8221;&#8217;</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Vincent Zalzal		</title>
		<link>https://www.fluentcpp.com/2018/10/26/how-to-access-the-index-of-the-current-element-in-a-modern-for-loop/#comment-1451</link>

		<dc:creator><![CDATA[Vincent Zalzal]]></dc:creator>
		<pubDate>Fri, 09 Nov 2018 04:49:00 +0000</pubDate>
		<guid isPermaLink="false">https://www.fluentcpp.com/?p=3918#comment-1451</guid>

					<description><![CDATA[In reply to &lt;a href=&quot;https://www.fluentcpp.com/2018/10/26/how-to-access-the-index-of-the-current-element-in-a-modern-for-loop/#comment-1448&quot;&gt;Jonathan Boccara&lt;/a&gt;.

As mentioned by Paul Baxter, Therocode&#039;s &quot;enumerate&quot; seems to do just that : https://blog.therocode.net/2018/10/for-each-with-index]]></description>
			<content:encoded><![CDATA[<p>In reply to <a href="https://www.fluentcpp.com/2018/10/26/how-to-access-the-index-of-the-current-element-in-a-modern-for-loop/#comment-1448">Jonathan Boccara</a>.</p>
<p>As mentioned by Paul Baxter, Therocode&#8217;s &#8220;enumerate&#8221; seems to do just that : <a href="https://blog.therocode.net/2018/10/for-each-with-index" rel="nofollow ugc">https://blog.therocode.net/2018/10/for-each-with-index</a></p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Jonathan Boccara		</title>
		<link>https://www.fluentcpp.com/2018/10/26/how-to-access-the-index-of-the-current-element-in-a-modern-for-loop/#comment-1448</link>

		<dc:creator><![CDATA[Jonathan Boccara]]></dc:creator>
		<pubDate>Thu, 08 Nov 2018 12:19:00 +0000</pubDate>
		<guid isPermaLink="false">https://www.fluentcpp.com/?p=3918#comment-1448</guid>

					<description><![CDATA[In reply to &lt;a href=&quot;https://www.fluentcpp.com/2018/10/26/how-to-access-the-index-of-the-current-element-in-a-modern-for-loop/#comment-1437&quot;&gt;Vincent Zalzal&lt;/a&gt;.

Yep, great idea! Would you like to try and code it up to see what this would look like?]]></description>
			<content:encoded><![CDATA[<p>In reply to <a href="https://www.fluentcpp.com/2018/10/26/how-to-access-the-index-of-the-current-element-in-a-modern-for-loop/#comment-1437">Vincent Zalzal</a>.</p>
<p>Yep, great idea! Would you like to try and code it up to see what this would look like?</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Vincent Zalzal		</title>
		<link>https://www.fluentcpp.com/2018/10/26/how-to-access-the-index-of-the-current-element-in-a-modern-for-loop/#comment-1437</link>

		<dc:creator><![CDATA[Vincent Zalzal]]></dc:creator>
		<pubDate>Mon, 05 Nov 2018 02:05:00 +0000</pubDate>
		<guid isPermaLink="false">https://www.fluentcpp.com/?p=3918#comment-1437</guid>

					<description><![CDATA[I like the boost solution a lot better. It is clean, safe and faster than the index function for non-random-access containers. It would be even more readable in my opinion by using structured bindings, like this :
&lt;pre&gt;&lt;code&gt;
    for (const auto&#038; [index, word] : words &#124; indexed(1))
    {
        std::cout &#060;&#060; index &#060;&#060; &#034; - &#034; &#060;&#060; word &#060;&#060; &#039;n&#039;;
    }
&lt;/code&gt;&lt;/pre&gt;

Unfortunately, this doesn&#039;t seem to work with boost indexed, but a modified version of it would work, if the dereferenced iterator would return a struct or standard pair or tuple.]]></description>
			<content:encoded><![CDATA[<p>I like the boost solution a lot better. It is clean, safe and faster than the index function for non-random-access containers. It would be even more readable in my opinion by using structured bindings, like this :</p>
<pre><code>
    for (const auto&amp; [index, word] : words | indexed(1))
    {
        std::cout &lt;&lt; index &lt;&lt; &quot; - &quot; &lt;&lt; word &lt;&lt; &#039;n&#039;;
    }
</code></pre>
<p>Unfortunately, this doesn&#8217;t seem to work with boost indexed, but a modified version of it would work, if the dereferenced iterator would return a struct or standard pair or tuple.</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Mario Not64		</title>
		<link>https://www.fluentcpp.com/2018/10/26/how-to-access-the-index-of-the-current-element-in-a-modern-for-loop/#comment-1420</link>

		<dc:creator><![CDATA[Mario Not64]]></dc:creator>
		<pubDate>Mon, 29 Oct 2018 05:23:00 +0000</pubDate>
		<guid isPermaLink="false">https://www.fluentcpp.com/?p=3918#comment-1420</guid>

					<description><![CDATA[Been using llvm&#039;s Enumerator.]]></description>
			<content:encoded><![CDATA[<p>Been using llvm&#8217;s Enumerator.</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Miguel Raggi		</title>
		<link>https://www.fluentcpp.com/2018/10/26/how-to-access-the-index-of-the-current-element-in-a-modern-for-loop/#comment-1419</link>

		<dc:creator><![CDATA[Miguel Raggi]]></dc:creator>
		<pubDate>Sun, 28 Oct 2018 09:10:00 +0000</pubDate>
		<guid isPermaLink="false">https://www.fluentcpp.com/?p=3918#comment-1419</guid>

					<description><![CDATA[One minor suggestion: In the following way you mentioned I&#039;ve found there is an error waiting to happen:
&lt;pre&gt;
 &lt;code class=&quot;javascript&quot;&gt;
    size_t i = 0;
    for (auto cons&#038; elem : container)
    {
        // do stuff
        ++i;
    }
 &lt;/code&gt;
&lt;/pre&gt;

The problem is that if there is a &quot;continue&quot; statement in the &quot;do part&quot;. In normal for loops when you continue the &quot;add one&quot; part still executes. But here it doesn&#039;t. So I find this a bit better:

&lt;pre&gt;
 &lt;code class=&quot;javascript&quot;&gt;
    int i = -1;
    for (auto cons&#038; elem : container)
    {
        ++i;
        // do stuff
    }
 &lt;/code&gt;
&lt;/pre&gt;

Well, in general one could use gsl::index or something similar instead of an int. Just not an unsigned type such as size_t]]></description>
			<content:encoded><![CDATA[<p>One minor suggestion: In the following way you mentioned I&#8217;ve found there is an error waiting to happen:</p>
<pre>
 <code class="javascript">
    size_t i = 0;
    for (auto cons&amp; elem : container)
    {
        // do stuff
        ++i;
    }
 </code>
</pre>
<p>The problem is that if there is a &#8220;continue&#8221; statement in the &#8220;do part&#8221;. In normal for loops when you continue the &#8220;add one&#8221; part still executes. But here it doesn&#8217;t. So I find this a bit better:</p>
<pre>
 <code class="javascript">
    int i = -1;
    for (auto cons&amp; elem : container)
    {
        ++i;
        // do stuff
    }
 </code>
</pre>
<p>Well, in general one could use gsl::index or something similar instead of an int. Just not an unsigned type such as size_t</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Malcolm Parsons		</title>
		<link>https://www.fluentcpp.com/2018/10/26/how-to-access-the-index-of-the-current-element-in-a-modern-for-loop/#comment-1417</link>

		<dc:creator><![CDATA[Malcolm Parsons]]></dc:creator>
		<pubDate>Sat, 27 Oct 2018 08:04:00 +0000</pubDate>
		<guid isPermaLink="false">https://www.fluentcpp.com/?p=3918#comment-1417</guid>

					<description><![CDATA[There&#039;s an open issue to add this to range-v3: https://github.com/ericniebler/range-v3/issues/785]]></description>
			<content:encoded><![CDATA[<p>There&#8217;s an open issue to add this to range-v3: <a href="https://github.com/ericniebler/range-v3/issues/785" rel="nofollow ugc">https://github.com/ericniebler/range-v3/issues/785</a></p>
]]></content:encoded>
		
			</item>
	</channel>
</rss>
