From 04041f9583a36d5de26cbbf9c8e4beaa7dcd1ca5 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Tue, 31 May 2022 16:08:11 +0200 Subject: [PATCH] util/Manual: use std::aligned_storage_t By using std::launder(), we can re-enable -Wstrict-aliasing. --- src/util/Manual.hxx | 24 +++++++----------------- 1 file changed, 7 insertions(+), 17 deletions(-) diff --git a/src/util/Manual.hxx b/src/util/Manual.hxx index b1b61df96..0496674f1 100644 --- a/src/util/Manual.hxx +++ b/src/util/Manual.hxx @@ -31,13 +31,9 @@ #include #include +#include #include -#if defined(__GNUC__) || defined(__clang__) -#pragma GCC diagnostic push -#pragma GCC diagnostic ignored "-Wstrict-aliasing" -#endif - /** * Container for an object that gets constructed and destructed * manually. The object is constructed in-place, and therefore @@ -46,8 +42,9 @@ */ template class Manual { - alignas(T) - char data[sizeof(T)]; + using Storage = std::aligned_storage_t; + + Storage storage; #ifndef NDEBUG bool initialized = false; @@ -77,8 +74,7 @@ public: void Construct(Args&&... args) { assert(!initialized); - void *p = data; - new(p) T(std::forward(args)...); + ::new(&storage) T(std::forward(args)...); #ifndef NDEBUG initialized = true; @@ -99,15 +95,13 @@ public: reference Get() noexcept { assert(initialized); - void *p = static_cast(data); - return *static_cast(p); + return *std::launder(reinterpret_cast(&storage)); } const_reference Get() const noexcept { assert(initialized); - const void *p = static_cast(data); - return *static_cast(p); + return *std::launder(reinterpret_cast(&storage)); } operator reference() noexcept { @@ -126,7 +120,3 @@ public: return &Get(); } }; - -#if defined(__GNUC__) || defined(__clang__) -#pragma GCC diagnostic pop -#endif