Skip to content

Commit

Permalink
[inplace_function] Really 100% stop using aligned_storage/alignment_of.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Quuxplusone committed Aug 21, 2024
1 parent 66692d1 commit 6a69c6e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 30 deletions.
23 changes: 11 additions & 12 deletions include/sg14/inplace_function.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,23 +51,22 @@ union aligned_storage_helper {
struct double4 { double a[4]; };
template<class T> using maybe = std::conditional_t<(Cap >= sizeof(T)), T, char>;
char real_data[Cap];
maybe<int> a;
maybe<long> b;
maybe<long long> c;
maybe<void*> d;
maybe<void(*)()> e;
maybe<double1> f;
maybe<double4> g;
maybe<long double> h;
maybe<short> a;
maybe<int> b;
maybe<long> c;
maybe<long long> d;
maybe<void*> e;
maybe<void(*)()> f;
maybe<double1> g;
maybe<double4> h;
maybe<long double> i;
};

template<size_t Cap, size_t Align = alignof(aligned_storage_helper<Cap>)>
struct aligned_storage {
using type = std::aligned_storage_t<Cap, Align>;
struct aligned_storage_t {
alignas(Align) char data_[Cap];
};

template<size_t Cap, size_t Align = alignof(aligned_storage_helper<Cap>)>
using aligned_storage_t = typename aligned_storage<Cap, Align>::type;
static_assert(sizeof(aligned_storage_t<sizeof(void*)>) == sizeof(void*), "A");
static_assert(alignof(aligned_storage_t<sizeof(void*)>) == alignof(void*), "B");
#else
Expand Down
24 changes: 6 additions & 18 deletions test/inplace_function_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -350,26 +350,14 @@ TEST(inplace_function, ExceptionSafety)
EXPECT_TRUE((caught) && (tf.countdown == 0) && (tf.constructed == 1) && (tf.destructed == 1));
}

template<size_t Cap>
static constexpr size_t expected_alignment_for_capacity()
{
constexpr size_t alignof_ptr = std::alignment_of<void*>::value;
constexpr size_t alignof_cap = std::alignment_of<std::aligned_storage_t<Cap>>::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<void(int), 1> >::value == expected_alignment_for_capacity<1>(), "");
static_assert(std::alignment_of< sg14::inplace_function<void(int), 2> >::value == expected_alignment_for_capacity<2>(), "");
static_assert(std::alignment_of< sg14::inplace_function<void(int), 4> >::value == expected_alignment_for_capacity<4>(), "");
static_assert(std::alignment_of< sg14::inplace_function<void(int), 8> >::value == expected_alignment_for_capacity<8>(), "");
static_assert(std::alignment_of< sg14::inplace_function<void(int), 16> >::value == expected_alignment_for_capacity<16>(), "");
static_assert(std::alignment_of< sg14::inplace_function<void(int), 32> >::value == expected_alignment_for_capacity<32>(), "");
static_assert(alignof(sg14::inplace_function<void(int), 1>) == alignof(void*), "");
static_assert(alignof(sg14::inplace_function<void(int), 2>) == alignof(void*), "");
static_assert(alignof(sg14::inplace_function<void(int), 4>) == alignof(void*), "");
static_assert(alignof(sg14::inplace_function<void(int), 8>) == alignof(void*), "");
static_assert(alignof(sg14::inplace_function<void(int), 16>) == alignof(void*), "");
static_assert(alignof(sg14::inplace_function<void(int), 32>) == alignof(void*), "");
static_assert(sizeof( sg14::inplace_function<void(int), sizeof(void*)> ) == 2 * sizeof(void*), "");
}

Expand Down

0 comments on commit 6a69c6e

Please sign in to comment.