From e996d3398feba3ca83987557ef59e1ce6973c90f Mon Sep 17 00:00:00 2001 From: Michele Caini Date: Thu, 18 Mar 2021 11:36:34 +0100 Subject: [PATCH] any: take in consideration alignment requirements for in_situ test (see #676) --- src/entt/core/any.hpp | 2 +- test/entt/core/any.cpp | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/src/entt/core/any.hpp b/src/entt/core/any.hpp index 8f1e3f377..bde585d46 100644 --- a/src/entt/core/any.hpp +++ b/src/entt/core/any.hpp @@ -28,7 +28,7 @@ class basic_any { using vtable_type = const void *(const operation, const basic_any &, const void *); template - static constexpr bool in_situ = Len && sizeof(Type) <= sizeof(storage_type) && std::is_nothrow_move_constructible_v; + static constexpr bool in_situ = Len && alignof(Type) <= alignof(storage_type) && sizeof(Type) <= sizeof(storage_type) && std::is_nothrow_move_constructible_v; template [[nodiscard]] static bool compare(const void *lhs, const void *rhs) { diff --git a/test/entt/core/any.cpp b/test/entt/core/any.cpp index 566247bce..f4453c7b2 100644 --- a/test/entt/core/any.cpp +++ b/test/entt/core/any.cpp @@ -34,6 +34,8 @@ struct not_copyable { double payload[Sz]; }; +struct alignas(64u) over_aligned {}; + TEST(Any, SBO) { entt::any any{'c'}; @@ -884,3 +886,10 @@ TEST(Any, SBOVsZeroedSBOSize) { ASSERT_EQ(valid, same.data()); } + +TEST(Any, Alignment) { + static constexpr auto alignment = alignof(over_aligned); + entt::basic_any target[2] = { over_aligned{}, over_aligned{} }; + ASSERT_TRUE((reinterpret_cast(entt::any_cast(&target[0u])) % alignment) == 0u); + ASSERT_TRUE((reinterpret_cast(entt::any_cast(&target[1u])) % alignment) == 0u); +}