[llvm] r354238 - [NFC] Better encapsulation of llvm::Optional Storage

Serge Guelton via llvm-commits llvm-commits at lists.llvm.org
Mon Feb 18 13:24:12 PST 2019


On Mon, Feb 18, 2019 at 12:18:21PM -0800, David Blaikie wrote:
> What was the UB/problem with the existing implementation (
> AlignedCharArrayUnion) - and/or is std::aligned_storage an option?

I'm unfortunately not 100% sure, but according to https://stackoverflow.com/questions/27003727/does-this-really-break-strict-aliasing-rules, the pattern

#include <iostream>

int main() 
{
   alignas(int) char data[sizeof(int)];
   int *myInt = new (data) int;
   *myInt = 34;

   std::cout << *reinterpret_cast<int*>(data);
}

is UB, and that's the exact pattern used in llvm::Optional.

looks like libcxx is using the following pattern, I'm trusting the expert on that subject :-)

   union { char empty; int some; } data;
   int *myInt = new (&data.some) int;
   *myInt = 34;

   std::cout << data.some;


More information about the llvm-commits mailing list