From 701a8ea1a48ac9a79e7fb6c0de9eda41a0dafbc1 Mon Sep 17 00:00:00 2001 From: Denis Blank Date: Mon, 29 Jan 2018 00:36:08 +0100 Subject: [PATCH] Attemt to fix the alignment errors --- include/continuable/detail/expected.hpp | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/include/continuable/detail/expected.hpp b/include/continuable/detail/expected.hpp index 9c624dd..c4cad6e 100644 --- a/include/continuable/detail/expected.hpp +++ b/include/continuable/detail/expected.hpp @@ -49,9 +49,11 @@ enum class slot_t { empty, value, error }; template using storage_of_t = // - std::aligned_storage_t<(sizeof(types::error_type) > sizeof(T) - ? sizeof(types::error_type) - : sizeof(T))>; + std::aligned_storage_t< + (sizeof(types::error_type) > sizeof(T) ? sizeof(types::error_type) + : sizeof(T)), + (alignof(types::error_type) > alignof(T) ? alignof(types::error_type) + : alignof(T))>; template struct expected_base { @@ -297,11 +299,27 @@ private: template V& cast() noexcept { assert(!is_empty()); + +#ifndef _NDEBUG + void* ptr = &this->storage_; + auto space = sizeof(this->storage_); + void* aligned = std::align(alignof(V), sizeof(V), ptr, space); + assert(aligned && "Bad alignment!"); +#endif // _NDEBUG + return *reinterpret_cast(&this->storage_); } template V const& cast() const noexcept { assert(!is_empty()); + +#ifndef _NDEBUG + void* ptr = const_cast(static_cast(&this->storage_)); + auto space = sizeof(this->storage_); + void* aligned = std::align(alignof(V), sizeof(V), ptr, space); + assert(aligned && "Bad alignment!"); +#endif // _NDEBUG + return *reinterpret_cast(&this->storage_); }