From 6a69c6e3974f9c877c3583e9bd994274b5cb2a1f Mon Sep 17 00:00:00 2001 From: Arthur O'Dwyer Date: Tue, 20 Aug 2024 18:18:57 -0400 Subject: [PATCH] [inplace_function] Really 100% stop using aligned_storage/alignment_of. Even when `SG14_USE_STD_ALIGNED_STORAGE` was false, there was still one use of `std::aligned_storage_t`, which is no longer OK in C++23 because `std::aligned_storage_t` is finally formally deprecated. Also, the unit tests were still relying on `alignment_of`, when they could just use the C++11 `alignof` keyword. Update that too. Finally, fix a defect: `sg14::aligned_storage_t<2>` had alignment 1 instead of alignment 2. This didn't affect `inplace_function`, though, because the function-pointer member dominates its observable alignment. --- include/sg14/inplace_function.h | 23 +++++++++++------------ test/inplace_function_test.cpp | 24 ++++++------------------ 2 files changed, 17 insertions(+), 30 deletions(-) diff --git a/include/sg14/inplace_function.h b/include/sg14/inplace_function.h index 96bab7d..a237660 100644 --- a/include/sg14/inplace_function.h +++ b/include/sg14/inplace_function.h @@ -51,23 +51,22 @@ union aligned_storage_helper { struct double4 { double a[4]; }; template using maybe = std::conditional_t<(Cap >= sizeof(T)), T, char>; char real_data[Cap]; - maybe a; - maybe b; - maybe c; - maybe d; - maybe e; - maybe f; - maybe g; - maybe h; + maybe a; + maybe b; + maybe c; + maybe d; + maybe e; + maybe f; + maybe g; + maybe h; + maybe i; }; template)> -struct aligned_storage { - using type = std::aligned_storage_t; +struct aligned_storage_t { + alignas(Align) char data_[Cap]; }; -template)> -using aligned_storage_t = typename aligned_storage::type; static_assert(sizeof(aligned_storage_t) == sizeof(void*), "A"); static_assert(alignof(aligned_storage_t) == alignof(void*), "B"); #else diff --git a/test/inplace_function_test.cpp b/test/inplace_function_test.cpp index 0d2c1ab..74bf042 100644 --- a/test/inplace_function_test.cpp +++ b/test/inplace_function_test.cpp @@ -350,26 +350,14 @@ TEST(inplace_function, ExceptionSafety) EXPECT_TRUE((caught) && (tf.countdown == 0) && (tf.constructed == 1) && (tf.destructed == 1)); } -template -static constexpr size_t expected_alignment_for_capacity() -{ - constexpr size_t alignof_ptr = std::alignment_of::value; - constexpr size_t alignof_cap = std::alignment_of>::value; -#define MIN(a,b) (a < b ? a : b) -#define MAX(a,b) (a > b ? a : b) - return MAX(MIN(Cap, alignof_cap), alignof_ptr); -#undef MAX -#undef MIN -} - TEST(inplace_function, StructLayout) { - static_assert(std::alignment_of< sg14::inplace_function >::value == expected_alignment_for_capacity<1>(), ""); - static_assert(std::alignment_of< sg14::inplace_function >::value == expected_alignment_for_capacity<2>(), ""); - static_assert(std::alignment_of< sg14::inplace_function >::value == expected_alignment_for_capacity<4>(), ""); - static_assert(std::alignment_of< sg14::inplace_function >::value == expected_alignment_for_capacity<8>(), ""); - static_assert(std::alignment_of< sg14::inplace_function >::value == expected_alignment_for_capacity<16>(), ""); - static_assert(std::alignment_of< sg14::inplace_function >::value == expected_alignment_for_capacity<32>(), ""); + static_assert(alignof(sg14::inplace_function) == alignof(void*), ""); + static_assert(alignof(sg14::inplace_function) == alignof(void*), ""); + static_assert(alignof(sg14::inplace_function) == alignof(void*), ""); + static_assert(alignof(sg14::inplace_function) == alignof(void*), ""); + static_assert(alignof(sg14::inplace_function) == alignof(void*), ""); + static_assert(alignof(sg14::inplace_function) == alignof(void*), ""); static_assert(sizeof( sg14::inplace_function ) == 2 * sizeof(void*), ""); }