<div dir="ltr">Fair enough. I understand the potential letter of the law regarding that approach being UB - CC'd Richard Smith in case he can correct/add some nuance to our understanding here.<br><br>std::aligned_storage still sounds like a good idea, though.<br><br>& I don't think LLVM is generalrly compiled with -fstrict-aliasing, is it? Do you know which compiler seems to be miscompiling these constructs?<br><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Mon, Feb 18, 2019 at 1:24 PM Serge Guelton <<a href="mailto:sguelton@redhat.com">sguelton@redhat.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">On Mon, Feb 18, 2019 at 12:18:21PM -0800, David Blaikie wrote:<br>
> What was the UB/problem with the existing implementation (<br>
> AlignedCharArrayUnion) - and/or is std::aligned_storage an option?<br>
<br>
I'm unfortunately not 100% sure, but according to <a href="https://stackoverflow.com/questions/27003727/does-this-really-break-strict-aliasing-rules" rel="noreferrer" target="_blank">https://stackoverflow.com/questions/27003727/does-this-really-break-strict-aliasing-rules</a>, the pattern<br>
<br>
#include <iostream><br>
<br>
int main() <br>
{<br>
   alignas(int) char data[sizeof(int)];<br>
   int *myInt = new (data) int;<br>
   *myInt = 34;<br>
<br>
   std::cout << *reinterpret_cast<int*>(data);<br>
}<br>
<br>
is UB, and that's the exact pattern used in llvm::Optional.<br>
<br>
looks like libcxx is using the following pattern, I'm trusting the expert on that subject :-)<br>
<br>
   union { char empty; int some; } data;<br>
   int *myInt = new (&data.some) int;<br>
   *myInt = 34;<br>
<br>
   std::cout << data.some;<br>
</blockquote></div></div>