On 26 July 2013 20:07, Jeffrey Yasskin <span dir="ltr">&lt;<a href="mailto:jyasskin@google.com" target="_blank">jyasskin@google.com</a>&gt;</span> wrote:<br><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">


<div><br>
</div>FWIW, you do need to deal with endian-ness issues in the real world,<br>
and simply overlaying a struct on raw bytes *won&#39;t deal with that*,<br></blockquote><div><br>Sure it will; just overlay a BigEndian&lt;uint16_t&gt; instead of a raw uint16_t.  Rough implementation:<br><br>// Assumes little endian machine<br>


template&lt;typename T&gt;<br>struct BigEndian<br>{<br>    operator T() const noexcept<br>    { return endian_swap(value); }<br><br>    BigEndian&amp; operator=(T t) noexcept<br>    { value = endian_swap(t); return *this; }<br>


<br>    T value;<br>};<br><br>endian_swap functionality is typically provided as a compiler built in and is starting to be included as a processor instruction (such as MOVBE on Haswell &lt;<a href="http://www.realworldtech.com/haswell-cpu/2/" target="_blank">http://www.realworldtech.com/haswell-cpu/2/</a>&gt;).  The efficiency is that important to low latency and embedded work.<br>

<br>Is this applicable to everything?  Of course not.  Serialization libraries are far more powerful, as on can use them with arbitrary objects, whereas type punning in this respect is only useful for, at best, trivially copyable types.<br>

<br>We really do need a clear statement of the current rules.  Is this code legal at all?  Is it legal if one just uses POD types?  Standard layout types?  Etc., etc.<br></div></div>-- <br> Nevin &quot;:-)&quot; Liber  &lt;mailto:<a href="mailto:nevin@eviloverlord.com" target="_blank">nevin@eviloverlord.com</a>&gt;  <a href="tel:%28847%29%20691-1404" value="+18476911404" target="_blank">(847) 691-1404</a>