<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Gerald Squelart</title>
    <description>The latest articles on DEV Community by Gerald Squelart (@squelart).</description>
    <link>https://dev.to/squelart</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F231683%2F158957c5-d059-4ede-9563-2eb9ef08395f.jpeg</url>
      <title>DEV Community: Gerald Squelart</title>
      <link>https://dev.to/squelart</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/squelart"/>
    <language>en</language>
    <item>
      <title>Avoid C++ implicit conversions from bool</title>
      <dc:creator>Gerald Squelart</dc:creator>
      <pubDate>Wed, 02 Oct 2019 06:48:45 +0000</pubDate>
      <link>https://dev.to/squelart/avoid-c-implicit-conversions-from-bool-14l5</link>
      <guid>https://dev.to/squelart/avoid-c-implicit-conversions-from-bool-14l5</guid>
      <description>&lt;p&gt;To most C++ programmers — or at least those like me with a short memory — it may feel like &lt;code&gt;bool&lt;/code&gt; has always been there. However it is still not present in its ancestor C (though C99 has &lt;code&gt;_Bool&lt;/code&gt;), and it was only introduced in C++98. At that time, &lt;a href="https://books.google.com.au/books?id=hS9mDwAAQBAJ&amp;amp;lpg=PT303&amp;amp;dq=%22design%20and%20evolution%20of%20c%2B%2B%22%20%22consider%20it%20absurd%20that%20anyone%20would%20bother%20to%20add%20such%20a%20type%20to%20C%2B%2B%22&amp;amp;pg=PT303#v=onepage&amp;amp;q=%22design%20and%20evolution%20of%20c++%22%20%22consider%20it%20absurd%20that%20anyone%20would%20bother%20to%20add%20such%20a%20type%20to%20C++%22&amp;amp;f=false"&gt;there was resistance to it&lt;/a&gt;, and examination of existing code (that was mostly using macros to simulate a boolean type) showed that &lt;a href="https://books.google.com.au/books?id=hS9mDwAAQBAJ&amp;amp;lpg=PT303&amp;amp;dq=%22design%20and%20evolution%20of%20c%2B%2B%22%20%22most%20Boolean%20types%20were%20used%20in%20ways%20that%20required%20free%20conversion%20to%20and%20from%20int%22&amp;amp;pg=PT303#v=onepage&amp;amp;q=%22design%20and%20evolution%20of%20c++%22%20%22most%20Boolean%20types%20were%20used%20in%20ways%20that%20required%20free%20conversion%20to%20and%20from%20int%22&amp;amp;f=false"&gt;preventing implicit conversion between boolean values and integer numbers would have broken a lot of things&lt;/a&gt;. So in C++, &lt;code&gt;bool&lt;/code&gt;-to-&lt;code&gt;int&lt;/code&gt; conversion is implicit, with &lt;a href="http://eel.is/c++draft/conv#prom-6"&gt;&lt;code&gt;false&lt;/code&gt; converting to 0 and &lt;code&gt;true&lt;/code&gt; to 1&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;I think that this implicit conversion from &lt;code&gt;bool&lt;/code&gt; is generally not needed in “real-world” code, and that it is in fact a potential source of bugs.&lt;/p&gt;

&lt;p&gt;For example, &lt;a href="https://twitter.com/nnethercote/status/1022422804937863168"&gt;this tweet&lt;/a&gt; shows that because of this implicit conversion, a typo like in &lt;code&gt;int x = 1 &amp;lt; y;&lt;/code&gt; (where &lt;code&gt;&amp;lt;&lt;/code&gt; was meant to be the left-shift operator &lt;code&gt;&amp;lt;&amp;lt;&lt;/code&gt;) didn’t raise any alarm.&lt;/p&gt;

&lt;p&gt;In my coding experience, I cannot think of useful cases where I would need to implicitly convert a &lt;code&gt;bool&lt;/code&gt; to something else, apart from &lt;code&gt;printf&lt;/code&gt; statements used during debugging — but for these, I have taken the habit of doing an explicit conversion anyway, e.g.: &lt;code&gt;printf("b=%d", int(b));&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;I can certainly imagine cases where a &lt;code&gt;bool&lt;/code&gt; could be converted to 0 or 1, to be used in a mathematical expression, e.g.: &lt;code&gt;a = useC * c&lt;/code&gt;, but I think this would be more clearly expressed as &lt;code&gt;a = useC ? c : 0&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Of course my experience is limited, and there may be genuine situations where this conversion could genuinely help — Please let me know. But I think that in these cases, making the conversion explicit should be trivial, and also useful by making it more obvious (&lt;code&gt;a = int(useC) * c&lt;/code&gt;). Now, if such conversion is needed for a lot of your code, maybe it’s a sign that the &lt;code&gt;bool&lt;/code&gt; type may not be the right choice.&lt;/p&gt;

&lt;p&gt;Note: clang-tidy offers &lt;a href="https://clang.llvm.org/extra/clang-tidy/checks/readability-implicit-bool-conversion.html"&gt;readability-implicit-bool-conversion&lt;/a&gt;, but by default it catches more conversions than just &lt;code&gt;bool&lt;/code&gt; to &lt;code&gt;int&lt;/code&gt;.&lt;/p&gt;




&lt;p&gt;&lt;em&gt;Originally posted on &lt;a href="https://tangentspaces.com/2018/09/22/avoid-cpp-implicit-conversions-from-bool/"&gt;Tangent Spaces&lt;/a&gt; -- I'm stealing my own content to try &lt;a href="https://dev.to/"&gt;dev.to&lt;/a&gt;, feedback welcome.&lt;/em&gt; 😺&lt;/p&gt;

</description>
      <category>cpp</category>
    </item>
  </channel>
</rss>
