<?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: A Case Where Using Auto Leads to Undefined Behaviour	</title>
	<atom:link href="https://www.fluentcpp.com/2018/10/30/case-where-auto-leads-to-undefined-behaviour/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.fluentcpp.com/2018/10/30/case-where-auto-leads-to-undefined-behaviour/</link>
	<description>Jonathan Boccara&#039;s blog</description>
	<lastBuildDate>Sun, 16 Aug 2020 14:27:41 +0000</lastBuildDate>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.5.5</generator>
	<item>
		<title>
		By: Vincent Zalzal		</title>
		<link>https://www.fluentcpp.com/2018/10/30/case-where-auto-leads-to-undefined-behaviour/#comment-1478</link>

		<dc:creator><![CDATA[Vincent Zalzal]]></dc:creator>
		<pubDate>Tue, 20 Nov 2018 05:38:00 +0000</pubDate>
		<guid isPermaLink="false">https://www.fluentcpp.com/?p=4089#comment-1478</guid>

					<description><![CDATA[In reply to &lt;a href=&quot;https://www.fluentcpp.com/2018/10/30/case-where-auto-leads-to-undefined-behaviour/#comment-1438&quot;&gt;Paweł Syska&lt;/a&gt;.

This way of writing decrementing unsigned loops is also usually generating better assembly code than the &quot;use i-1 inside loop&quot; method or the &quot;compare to SIZE_MAX&quot; method.]]></description>
			<content:encoded><![CDATA[<p>In reply to <a href="https://www.fluentcpp.com/2018/10/30/case-where-auto-leads-to-undefined-behaviour/#comment-1438">Paweł Syska</a>.</p>
<p>This way of writing decrementing unsigned loops is also usually generating better assembly code than the &#8220;use i-1 inside loop&#8221; method or the &#8220;compare to SIZE_MAX&#8221; method.</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Paweł Syska		</title>
		<link>https://www.fluentcpp.com/2018/10/30/case-where-auto-leads-to-undefined-behaviour/#comment-1438</link>

		<dc:creator><![CDATA[Paweł Syska]]></dc:creator>
		<pubDate>Mon, 05 Nov 2018 14:00:00 +0000</pubDate>
		<guid isPermaLink="false">https://www.fluentcpp.com/?p=4089#comment-1438</guid>

					<description><![CDATA[You can also solve this by writing:
for(auto i = bits.size(); i-- &#062; 0;) {
}]]></description>
			<content:encoded><![CDATA[<p>You can also solve this by writing:<br />
for(auto i = bits.size(); i&#8211; &gt; 0;) {<br />
}</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Roman Granacher		</title>
		<link>https://www.fluentcpp.com/2018/10/30/case-where-auto-leads-to-undefined-behaviour/#comment-1435</link>

		<dc:creator><![CDATA[Roman Granacher]]></dc:creator>
		<pubDate>Sat, 03 Nov 2018 20:49:00 +0000</pubDate>
		<guid isPermaLink="false">https://www.fluentcpp.com/?p=4089#comment-1435</guid>

					<description><![CDATA[Hey, thanks for the interesting post. I have to join the other commenters but I want to add the following: There is not actually any undefined behavior involved here. Everything is perfectly defined:
1. &quot;bits.size() - 1&quot; is whatever size_t is, which here is probably unsigned long, due to the usual arithmetic conversions.
2. Afterwards auto deduces its type perfectly in according with the standard to unsigned long.
3. Due to the &#060;= you decrease an unsigned type below zero which is defined in the standard to behave as module arithmetic

From: https://en.cppreference.com/w/cpp/language/operator_arithmetic#Conversions
&#034;Unsigned integer arithmetic is always performed modulo 2n
 where n is the number of bits in that particular integer. E.g. for unsigned int, adding one to UINT_MAX gives ​0​, and subtracting one from ​0​ gives UINT_MAX.

When signed integer arithmetic operation overflows (the result does not fit in the result type), the behavior is undefined:&#034;

Therefore everything here is perfectly defined (so maybe you can change the title). It is just a user introduced bug. Still I am always happy to read your blog and discuss coding. Keep up the good work!]]></description>
			<content:encoded><![CDATA[<p>Hey, thanks for the interesting post. I have to join the other commenters but I want to add the following: There is not actually any undefined behavior involved here. Everything is perfectly defined:<br />
1. &#8220;bits.size() &#8211; 1&#8221; is whatever size_t is, which here is probably unsigned long, due to the usual arithmetic conversions.<br />
2. Afterwards auto deduces its type perfectly in according with the standard to unsigned long.<br />
3. Due to the &lt;= you decrease an unsigned type below zero which is defined in the standard to behave as module arithmetic</p>
<p>From: <a href="https://en.cppreference.com/w/cpp/language/operator_arithmetic#Conversions" rel="nofollow ugc">https://en.cppreference.com/w/cpp/language/operator_arithmetic#Conversions</a><br />
&quot;Unsigned integer arithmetic is always performed modulo 2n<br />
 where n is the number of bits in that particular integer. E.g. for unsigned int, adding one to UINT_MAX gives ​0​, and subtracting one from ​0​ gives UINT_MAX.</p>
<p>When signed integer arithmetic operation overflows (the result does not fit in the result type), the behavior is undefined:&quot;</p>
<p>Therefore everything here is perfectly defined (so maybe you can change the title). It is just a user introduced bug. Still I am always happy to read your blog and discuss coding. Keep up the good work!</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: 康小广		</title>
		<link>https://www.fluentcpp.com/2018/10/30/case-where-auto-leads-to-undefined-behaviour/#comment-1429</link>

		<dc:creator><![CDATA[康小广]]></dc:creator>
		<pubDate>Wed, 31 Oct 2018 18:14:00 +0000</pubDate>
		<guid isPermaLink="false">https://www.fluentcpp.com/?p=4089#comment-1429</guid>

					<description><![CDATA[What about using `std::npos` instead? Anyway it seems having nothing to do with `auto`]]></description>
			<content:encoded><![CDATA[<p>What about using `std::npos` instead? Anyway it seems having nothing to do with `auto`</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Colin Pitrat		</title>
		<link>https://www.fluentcpp.com/2018/10/30/case-where-auto-leads-to-undefined-behaviour/#comment-1428</link>

		<dc:creator><![CDATA[Colin Pitrat]]></dc:creator>
		<pubDate>Wed, 31 Oct 2018 09:31:00 +0000</pubDate>
		<guid isPermaLink="false">https://www.fluentcpp.com/?p=4089#comment-1428</guid>

					<description><![CDATA[I also disagree that auto is the problem !
Note that you get a warning for the comparison always being true.]]></description>
			<content:encoded><![CDATA[<p>I also disagree that auto is the problem !<br />
Note that you get a warning for the comparison always being true.</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Gerardo Pérez		</title>
		<link>https://www.fluentcpp.com/2018/10/30/case-where-auto-leads-to-undefined-behaviour/#comment-1427</link>

		<dc:creator><![CDATA[Gerardo Pérez]]></dc:creator>
		<pubDate>Tue, 30 Oct 2018 15:32:00 +0000</pubDate>
		<guid isPermaLink="false">https://www.fluentcpp.com/?p=4089#comment-1427</guid>

					<description><![CDATA[There is another bug, you will flip twice the bit when bits.size() == 1]]></description>
			<content:encoded><![CDATA[<p>There is another bug, you will flip twice the bit when bits.size() == 1</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Marius Bancila		</title>
		<link>https://www.fluentcpp.com/2018/10/30/case-where-auto-leads-to-undefined-behaviour/#comment-1426</link>

		<dc:creator><![CDATA[Marius Bancila]]></dc:creator>
		<pubDate>Tue, 30 Oct 2018 11:25:00 +0000</pubDate>
		<guid isPermaLink="false">https://www.fluentcpp.com/?p=4089#comment-1426</guid>

					<description><![CDATA[I disagree that auto is the problem here. You have the save behavior if you write size_t or unsigned int. The problem is how the loop was written, as already mentioned in the comments, so I won&#039;t repeat that.]]></description>
			<content:encoded><![CDATA[<p>I disagree that auto is the problem here. You have the save behavior if you write size_t or unsigned int. The problem is how the loop was written, as already mentioned in the comments, so I won&#8217;t repeat that.</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: jft		</title>
		<link>https://www.fluentcpp.com/2018/10/30/case-where-auto-leads-to-undefined-behaviour/#comment-1425</link>

		<dc:creator><![CDATA[jft]]></dc:creator>
		<pubDate>Tue, 30 Oct 2018 10:46:00 +0000</pubDate>
		<guid isPermaLink="false">https://www.fluentcpp.com/?p=4089#comment-1425</guid>

					<description><![CDATA[If the loop had been written as 

&lt;pre&gt;&lt;code&gt;
for (unsigned int bitIndex = bits.size() - 1; bitIndex &#062;= 0; --bitIndex)

&lt;/code&gt;&lt;/pre&gt;

Then this is a well known c/c++ idiom. The type of bitindex is correct as an index shouldn&#039;t be negative - but as is detailed above, in this loop implementation bitindex can go negative which means a large positive number as an unsigned number can&#039;t be negative.

The &#039;standard&#039; solution to this is to rewrite the loop (as mentioned by Gerald) so that bitindex can never be negative - not to make bitindex an int.

&lt;pre&gt;&lt;code&gt;
for (auto bitIndex = bits.size(); bitIndex &#062; 0; --bitIndex)
{
    auto&#038; bit = bits[bitIndex - 1];

    flip(bit);

            if (bit == true)
        break;
}
&lt;/code&gt;&lt;/pre&gt;

As has been pointed out, this has absolutely nothing to do with auto which is blameless in this matter.]]></description>
			<content:encoded><![CDATA[<p>If the loop had been written as </p>
<pre><code>
for (unsigned int bitIndex = bits.size() - 1; bitIndex &gt;= 0; --bitIndex)

</code></pre>
<p>Then this is a well known c/c++ idiom. The type of bitindex is correct as an index shouldn&#8217;t be negative &#8211; but as is detailed above, in this loop implementation bitindex can go negative which means a large positive number as an unsigned number can&#8217;t be negative.</p>
<p>The &#8216;standard&#8217; solution to this is to rewrite the loop (as mentioned by Gerald) so that bitindex can never be negative &#8211; not to make bitindex an int.</p>
<pre><code>
for (auto bitIndex = bits.size(); bitIndex &gt; 0; --bitIndex)
{
    auto&amp; bit = bits[bitIndex - 1];

    flip(bit);

            if (bit == true)
        break;
}
</code></pre>
<p>As has been pointed out, this has absolutely nothing to do with auto which is blameless in this matter.</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: frachop		</title>
		<link>https://www.fluentcpp.com/2018/10/30/case-where-auto-leads-to-undefined-behaviour/#comment-1424</link>

		<dc:creator><![CDATA[frachop]]></dc:creator>
		<pubDate>Tue, 30 Oct 2018 09:10:00 +0000</pubDate>
		<guid isPermaLink="false">https://www.fluentcpp.com/?p=4089#comment-1424</guid>

					<description><![CDATA[Thank you for sharing !!
just a spelling issue : &quot;This causes undefined behaviour, which occurs in the form of a set (SEG) fault in this case&quot;]]></description>
			<content:encoded><![CDATA[<p>Thank you for sharing !!<br />
just a spelling issue : &#8220;This causes undefined behaviour, which occurs in the form of a set (SEG) fault in this case&#8221;</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Flamefire		</title>
		<link>https://www.fluentcpp.com/2018/10/30/case-where-auto-leads-to-undefined-behaviour/#comment-1423</link>

		<dc:creator><![CDATA[Flamefire]]></dc:creator>
		<pubDate>Tue, 30 Oct 2018 08:47:00 +0000</pubDate>
		<guid isPermaLink="false">https://www.fluentcpp.com/?p=4089#comment-1423</guid>

					<description><![CDATA[Note: Just enable `-Wextra` and you get a clear warning pointing you to the exact problem.]]></description>
			<content:encoded><![CDATA[<p>Note: Just enable `-Wextra` and you get a clear warning pointing you to the exact problem.</p>
]]></content:encoded>
		
			</item>
	</channel>
</rss>
