On 26 July 2013 13:35, Jeffrey Yasskin <span dir="ltr"><<a href="mailto:jyasskin@google.com" target="_blank">jyasskin@google.com</a>></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">
<br>
Ah, that's too bad because it doesn't serve the purpose Ion was asking<br>
about. He wanted a memory buffer with known contents to become the<br>
object representation of an object of a particular type.</blockquote><div><br>+1.<br><br>This is a real issue for anyone that has to interpret binary data from a hardware device, a network connection or a file. There needs to be an easy to program way of accomplishing this; one shouldn't have to consult the C++ Committee or be an expert on undefined behavior to guess if the code is correct, and one shouldn't have to manually look at the data byte by byte to convert it. (Besides the copying cost, one may not need all the fields in a message, yet it is incredibly error prone to convert just the fields you do need.)<br>
<br>People use the union hack because it is easy to program, given that it is mainly declarative. Declare a struct that corresponds to the layout of the data, cast the buffer pointer to the struct pointer, and it usually just works. If it isn't that easy, people will just keep using the union hack.<br>
<br>This is one of the reasons people pick C and C++ in the first place.<br><br>One (unexplored) thought: maybe we need some magic classes to encapsulate this, just like std::atomic hides details about atomic access.<br clear="all">
</div></div>-- <br> Nevin ":-)" Liber <mailto:<a href="mailto:nevin@eviloverlord.com" target="_blank">nevin@eviloverlord.com</a>> (847) 691-1404