Option: Use __builtin_launder to ensure that the compiler doesn't label this as UB

This commit is contained in:
apio 2023-02-14 20:03:29 +01:00
parent 79078ff21e
commit 6eff41e7af
Signed by: apio
GPG Key ID: B8A7D06E42258954

View File

@ -138,26 +138,16 @@ template <typename T> class Option
private: private:
struct Storage struct Storage
{ {
u8 buffer[sizeof(T)]; alignas(T) u8 buffer[sizeof(T)];
T* fetch_ptr()
{
return (T*)buffer;
}
T& fetch_reference() T& fetch_reference()
{ {
return *fetch_ptr(); return *__builtin_launder(reinterpret_cast<T*>(&buffer));
}
const T* fetch_ptr() const
{
return (const T*)buffer;
} }
const T& fetch_reference() const const T& fetch_reference() const
{ {
return *fetch_ptr(); return *__builtin_launder(reinterpret_cast<const T*>(&buffer));
} }
void store_reference(const T& ref) void store_reference(const T& ref)