title: Can Aliasing Problems be Avoided with const Variables
body: <p>My company uses a messaging server which gets a message into a <code>const char*</code> and then casts it to the message type.</p>
  
  <p>I've become concerned about this after asking <a href="https://stackoverflow.com/q/28697626/2642059">this question</a>. I'm not aware of any bad behavior in the messaging server. Is it possible that <code>const</code> variables do not incur aliasing problems?</p>
  
  <p>For example say that foo is defined in <code>MessageServer</code> in one of these ways:</p>
  
  <ol>
  <li>As a parameter: <code>void MessageServer(const char* foo)</code></li>
  <li>Or as const variable at the top of <code>MessageServer</code>: <code>const char* foo = PopMessage();</code></li>
  </ol>
  
  <p>Now <code>MessageServer</code> is a huge function, but it never assigns anything to <code>foo</code>, however at 1 point in <code>MessageServer</code>'s logic <code>foo</code> <em>will</em> be cast to the selected message type.</p>
  
  <pre><code>auto bar = reinterpret_cast&lt;const MessageJ*&gt;(foo);
  </code></pre>
  
  <p><code>bar</code> will only be read from subsequently, but will be used extensively for object setup.</p>
  
  <p>Is an aliasing problem possible here, or does the fact that <code>foo</code> is only initialized, and never modified save me?</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p><a href="https://stackoverflow.com/a/29122056/2642059">Jarod42's answer</a> finds no problem with casting from a <code>const char*</code> to a <code>MessageJ*</code>, but I'm not sure this makes sense.</p>
  
  <p>We know this is illegal:</p>
  
  <pre><code>MessageX* foo = new MessageX;
  const auto bar = reinterpret_cast&lt;MessageJ*&gt;(foo);
  </code></pre>
  
  <p>Are we saying this somehow makes it legal?</p>
  
  <pre><code>MessageX* foo = new MessageX;
  const auto temp = reinterpret_cast&lt;char*&gt;(foo);
  auto bar = reinterpret_cast&lt;const MessageJ*&gt;(temp);
  </code></pre>
  
  <p>My understanding of <a href="https://stackoverflow.com/a/29122056/2642059">Jarod42's answer</a> is that the cast to <code>temp</code> makes it legal.</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>I've gotten some comments with relation to serialization, alignment, network passing, and so on. That's not what this question is about.</p>
  
  <p><strong>This is a question about <a href="http://cellperformance.beyond3d.com/articles/2006/06/understanding-strict-aliasing.html" rel="nofollow noreferrer">strict aliasing</a>.</strong></p>
  
  <blockquote>
    <p>Strict aliasing is an assumption, made by the C (or C++) compiler, that dereferencing pointers to objects of different types will never refer to the same memory location (i.e. alias eachother.)</p>
  </blockquote>
  
  <p>What I'm asking is: <strong>Will the initialization of a <code>const</code> object, by casting from a <code>char*</code>, ever be optimized below where that object is cast to another type of object, such that I am casting from uninitialized data?</strong></p>