From c2ad28d702c2acd1cc62771a1f3dd05523a7d6c6 Mon Sep 17 00:00:00 2001 From: Denis Blank Date: Mon, 29 Jan 2018 00:36:08 +0100 Subject: [PATCH] More is aligned tests --- include/continuable/detail/expected.hpp | 35 ++++++++++++++++++------- 1 file changed, 25 insertions(+), 10 deletions(-) diff --git a/include/continuable/detail/expected.hpp b/include/continuable/detail/expected.hpp index c4cad6e..ac363c1 100644 --- a/include/continuable/detail/expected.hpp +++ b/include/continuable/detail/expected.hpp @@ -43,6 +43,13 @@ namespace cti { namespace detail { +#ifndef _NDEBUG +template +bool is_aligned(T const* p) { + return !(reinterpret_cast(p) % std::alignment_of::value); +} +#endif // _NDEBUG + namespace util { namespace detail { enum class slot_t { empty, value, error }; @@ -301,26 +308,34 @@ private: 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!"); + { + 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_); + auto ptr = reinterpret_cast(&this->storage_); + assert(is_aligned(ptr) && "ptr not aligned!"); + return *ptr; } 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!"); + { + 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_); + auto ptr = reinterpret_cast(&this->storage_); + assert(is_aligned(ptr) && "ptr not aligned!"); + return *ptr; } template