Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use alignas instead of std::aligned_storage #830

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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