Skip to content

Commit

Permalink
Use alignas instead of std::aligned_storage
Browse files Browse the repository at this point in the history
  • Loading branch information
Marc Kramer committed Aug 17, 2024
1 parent d1fcec8 commit 393efbc
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions include/cereal/types/memory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,10 @@ namespace cereal
// typedefs for parent type and storage type
using BaseType = typename ::cereal::traits::get_shared_from_this_base<T>::type;
using ParentType = std::enable_shared_from_this<BaseType>;
using StorageType = typename std::aligned_storage<sizeof(ParentType), CEREAL_ALIGNOF(ParentType)>::type;
class alignas(ParentType) StorageType {
private:
std::byte data[sizeof(ParentType)];
};

public:
//! Saves the state of some type inheriting from enable_shared_from_this
Expand Down Expand Up @@ -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<sizeof(T), CEREAL_ALIGNOF(T)>::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
Expand Down Expand Up @@ -377,7 +383,10 @@ namespace cereal
using NonConstT = typename std::remove_const<T>::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<sizeof(NonConstT), CEREAL_ALIGNOF(NonConstT)>::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
Expand Down

0 comments on commit 393efbc

Please sign in to comment.