How do I interpret a message payload without violating type aliasing rules? : <p>My program receives messages over the network. These messages are deserialized by some middleware (i.e. someone else's code which I cannot change). My program receives objects that look something like this:</p>

<pre><code>struct Message {
    int msg_type;
    std::vector&lt;uint8_t&gt; payload;
};
</code></pre>

<p>By examining <code>msg_type</code> I can determine that the message payload is actually, for example, an array of <code>uint16_t</code> values. I would like to read that array without an unnecessary copy.</p>

<p>My first thought was to do this:</p>

<pre><code>const uint16_t* a = reinterpret_cast&lt;uint16_t*&gt;(msg.payload.data());
</code></pre>

<p>But then reading from <code>a</code> would appear to violate the standard. Here is clause 3.10.10:</p>

<blockquote>
  <p>If a program attempts to access the stored value of an object through a glvalue of other than one of the following types the behavior is undefined:</p>
  
  <ul>
  <li>the dynamic type of the object,</li>
  <li>a cv-qualified version of the dynamic type of the object,</li>
  <li>a type similar (as defined in 4.4) to the dynamic type of the object,</li>
  <li>a type that is the signed or unsigned type corresponding to the dynamic type of the object,</li>
  <li>a type that is the signed or unsigned type corresponding to a cv-qualified version of the dynamic type of the object,</li>
  <li>an aggregate or union type that includes one of the aforementioned types among its elements or nonstatic data members (including, recursively, an element or non-static data member of a subaggregate or contained union),</li>
  <li>a type that is a (possibly cv-qualified) base class type of the dynamic type of the object,</li>
  <li>a <code>char</code> or <code>unsigned char</code> type.</li>
  </ul>
</blockquote>

<p>In this case, <code>a</code> would be the glvalue and <code>uint16_t*</code> does not appear to meet any of the listed criteria.</p>

<p>So how do I treat the payload as an array of <code>uint16_t</code> values without invoking undefined behavior or performing an unnecessary copy?</p>
