diff --git a/util/env_posix.cc b/util/env_posix.cc index fac41be6ceb..86571059baf 100644 --- a/util/env_posix.cc +++ b/util/env_posix.cc @@ -854,9 +854,13 @@ class SingletonEnv { #endif // !defined(NDEBUG) static_assert(sizeof(env_storage_) >= sizeof(EnvType), "env_storage_ will not fit the Env"); - static_assert(alignof(decltype(env_storage_)) >= alignof(EnvType), + static_assert(std::is_standard_layout_v>); + static_assert( + offsetof(SingletonEnv, env_storage_) % alignof(EnvType) == 0, + "env_storage_ does not meet the Env's alignment needs"); + static_assert(alignof(SingletonEnv) % alignof(EnvType) == 0, "env_storage_ does not meet the Env's alignment needs"); - new (&env_storage_) EnvType(); + new (env_storage_) EnvType(); } ~SingletonEnv() = default; @@ -872,8 +876,7 @@ class SingletonEnv { } private: - typename std::aligned_storage::type - env_storage_; + alignas(EnvType) char env_storage_[sizeof(EnvType)]; #if !defined(NDEBUG) static std::atomic env_initialized_; #endif // !defined(NDEBUG) diff --git a/util/env_windows.cc b/util/env_windows.cc index aafcdcc3be5..0a48c3fb52b 100644 --- a/util/env_windows.cc +++ b/util/env_windows.cc @@ -802,9 +802,13 @@ class SingletonEnv { #endif // !defined(NDEBUG) static_assert(sizeof(env_storage_) >= sizeof(EnvType), "env_storage_ will not fit the Env"); - static_assert(alignof(decltype(env_storage_)) >= alignof(EnvType), + static_assert(std::is_standard_layout_v>); + static_assert( + offsetof(SingletonEnv, env_storage_) % alignof(EnvType) == 0, + "env_storage_ does not meet the Env's alignment needs"); + static_assert(alignof(SingletonEnv) % alignof(EnvType) == 0, "env_storage_ does not meet the Env's alignment needs"); - new (&env_storage_) EnvType(); + new (env_storage_) EnvType(); } ~SingletonEnv() = default; @@ -820,8 +824,7 @@ class SingletonEnv { } private: - typename std::aligned_storage::type - env_storage_; + alignas(EnvType) char env_storage_[sizeof(EnvType)]; #if !defined(NDEBUG) static std::atomic env_initialized_; #endif // !defined(NDEBUG) diff --git a/util/no_destructor.h b/util/no_destructor.h index a0d3b8703d7..c28a1073135 100644 --- a/util/no_destructor.h +++ b/util/no_destructor.h @@ -5,6 +5,7 @@ #ifndef STORAGE_LEVELDB_UTIL_NO_DESTRUCTOR_H_ #define STORAGE_LEVELDB_UTIL_NO_DESTRUCTOR_H_ +#include #include #include @@ -20,10 +21,14 @@ class NoDestructor { explicit NoDestructor(ConstructorArgTypes&&... constructor_args) { static_assert(sizeof(instance_storage_) >= sizeof(InstanceType), "instance_storage_ is not large enough to hold the instance"); + static_assert(std::is_standard_layout_v>); static_assert( - alignof(decltype(instance_storage_)) >= alignof(InstanceType), + offsetof(NoDestructor, instance_storage_) % alignof(InstanceType) == 0, "instance_storage_ does not meet the instance's alignment requirement"); - new (&instance_storage_) + static_assert( + alignof(NoDestructor) % alignof(InstanceType) == 0, + "instance_storage_ does not meet the instance's alignment requirement"); + new (instance_storage_) InstanceType(std::forward(constructor_args)...); } @@ -37,8 +42,7 @@ class NoDestructor { } private: - typename std::aligned_storage::type instance_storage_; + alignas(InstanceType) char instance_storage_[sizeof(InstanceType)]; }; } // namespace leveldb