<?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: Bastien JAUNY</title>
    <description>The latest articles on DEV Community by Bastien JAUNY (@bjauny).</description>
    <link>https://dev.to/bjauny</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%2F242502%2F752fbebb-1032-40d2-8172-46565f3475bb.jpeg</url>
      <title>DEV Community: Bastien JAUNY</title>
      <link>https://dev.to/bjauny</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/bjauny"/>
    <language>en</language>
    <item>
      <title>The const issue</title>
      <dc:creator>Bastien JAUNY</dc:creator>
      <pubDate>Thu, 17 Oct 2019 12:46:48 +0000</pubDate>
      <link>https://dev.to/bjauny/the-const-issue-2bok</link>
      <guid>https://dev.to/bjauny/the-const-issue-2bok</guid>
      <description>&lt;p&gt;TLDR: &lt;em&gt;always put &lt;code&gt;const&lt;/code&gt; AFTER the element you want to specify as constant&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;We all love &lt;code&gt;const&lt;/code&gt; variables and parameters. They are, well, constant. You're sure they won't be changed by a side-effect of your code (or someone else's).&lt;br&gt;
What you usually see in C++ is something like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const int myVar(42);
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This is pretty clear, right? &lt;code&gt;myVar&lt;/code&gt; is stores an integer, 42 in this case, and this value won't change until we get out of the current scope. So if we put something like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const int myVar(42);
myVar++;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;your compiler will yell at you with something like &lt;code&gt;error: increment of read-only variable ‘myVar’&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;So far so good. But I once again stumbled across an issue using &lt;code&gt;const&lt;/code&gt; that I, even with my 10+ years of experience, still haven't stuck in my head (but I hope that writing this down will help so I'll guess I should share it).&lt;/p&gt;

&lt;p&gt;Let's say you want to declare a constant pointer to an integer. The no-brainer line of code that gets out is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const int *myVarPtr;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Following the same logic as before, if I try to modify it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;int myVar(42);
const int *myVarPtr;
myVarPtr++;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;the compiler doesn't complain! And it's normal because, if you stick to its definition, &lt;code&gt;const&lt;/code&gt; applies to the element to its &lt;em&gt;left&lt;/em&gt;, unless it's the first element. Then it applies to its &lt;em&gt;right&lt;/em&gt;.&lt;br&gt;
So what we declared earlier was actually a non-const pointer to a const integer. The original goal would be achieved with this code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;int * const myVarPtr;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;I hate exceptions. This is the typical case where the exception makes the concept confusing. Now I just make sure I'm never in this case.&lt;/p&gt;

&lt;p&gt;If you have only one thing to learn from this article it would be this: &lt;em&gt;always put &lt;code&gt;const&lt;/code&gt; AFTER the element you want to specify as constant&lt;/em&gt;.&lt;/p&gt;

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