From db01bb0eed347a8aee44d7a9536389dc06af8392 Mon Sep 17 00:00:00 2001 From: Marc Kramer Date: Mon, 12 Aug 2024 21:15:19 +0200 Subject: [PATCH] Use alignas instead of std::aligned_storage --- include/cereal/types/memory.hpp | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/include/cereal/types/memory.hpp b/include/cereal/types/memory.hpp index 3e2f63b2..618cf829 100644 --- a/include/cereal/types/memory.hpp +++ b/include/cereal/types/memory.hpp @@ -134,7 +134,10 @@ namespace cereal // typedefs for parent type and storage type using BaseType = typename ::cereal::traits::get_shared_from_this_base::type; using ParentType = std::enable_shared_from_this; - using StorageType = typename std::aligned_storage::type; + class alignas(ParentType) StorageType { + private: + std::byte data[sizeof(ParentType)]; + }; public: //! Saves the state of some type inheriting from enable_shared_from_this @@ -286,7 +289,10 @@ namespace cereal { // Storage type for the pointer - since we can't default construct this type, // we'll allocate it using std::aligned_storage and use a custom deleter - using AlignedStorage = typename std::aligned_storage::type; + class alignas(T) AlignedStorage { + private: + std::byte data[sizeof(T)]; + }; // Valid flag - set to true once construction finishes // This prevents us from calling the destructor on @@ -377,7 +383,10 @@ namespace cereal using NonConstT = typename std::remove_const::type; // Storage type for the pointer - since we can't default construct this type, // we'll allocate it using std::aligned_storage - using AlignedStorage = typename std::aligned_storage::type; + class alignas(NonConstT) AlignedStorage { + private: + std::byte data[sizeof(NonConstT)]; + }; // Allocate storage - note the AlignedStorage type so that deleter is correct if // an exception is thrown before we are initialized