Skip to content

Commit c650a4d

Browse files
committed
[utils] Deprecate modm::aligned_storage_t
Deprecate modm::aligned_storage_t which relies on the deprecated type std::aligned_storage. Remove all usages from modm and replace them with a properly aligned std::byte array as recommended by C++ paper P1413R3.
1 parent 635acfe commit c650a4d

5 files changed

Lines changed: 65 additions & 10 deletions

File tree

src/modm/utils/aligned_storage.hpp

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ union aligned_storage_helper
5151
};
5252
} // namespace aligned_storage_impl
5353

54+
template<std::size_t Cap>
55+
constexpr auto default_storage_alignment = alignof(aligned_storage_impl::aligned_storage_helper<Cap>);
56+
5457
/**
5558
* Implementation of std::aligned_storage that avoids GCC bug #61458 which can
5659
* cause excessive size for types smaller than the maximum alignment.
@@ -59,15 +62,20 @@ union aligned_storage_helper
5962
* The implementation is derived from:
6063
* https://github.com/WG21-SG14/SG14/blob/master/SG14/inplace_function.h
6164
*/
62-
template<size_t Cap, size_t Align = alignof(aligned_storage_impl::aligned_storage_helper<Cap>)>
63-
struct aligned_storage {
64-
using type = std::aligned_storage_t<Cap, Align>;
65+
template<std::size_t Cap, std::size_t Align = default_storage_alignment<Cap>>
66+
struct [[deprecated("see C++ standards paper P1413R3")]] aligned_storage // DEPRECATED: 2026q3
67+
{
68+
struct type
69+
{
70+
alignas(Align) unsigned char data[Cap];
71+
};
6572
};
6673
/// @endcond
6774

6875
/// @ingroup modm_utils
69-
template<size_t Cap, size_t Align = alignof(aligned_storage_impl::aligned_storage_helper<Cap>)>
70-
using aligned_storage_t = typename aligned_storage<Cap, Align>::type;
76+
template<std::size_t Cap, std::size_t Align = default_storage_alignment<Cap>>
77+
using aligned_storage_t [[deprecated("see C++ standards paper P1413R3")]] =
78+
typename aligned_storage<Cap, Align>::type; // DEPRECATED: 2026q3
7179

7280
static_assert(sizeof(aligned_storage_t<sizeof(void*)>) == sizeof(void*));
7381
static_assert(alignof(aligned_storage_t<sizeof(void*)>) == alignof(void*));

src/modm/utils/inplace_any.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ class inplace_any final
207207
void move_from(inplace_any<S>&& other) noexcept;
208208

209209
private:
210-
modm::aligned_storage<Size>::type storage_;
210+
alignas(default_storage_alignment<Size>) std::byte storage_[Size];
211211
inplace_any_impl::HandlerFunc handler_{nullptr};
212212
};
213213

src/modm/utils/inplace_function.hpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
#pragma once
2828

29+
#include <cstddef>
2930
#include <type_traits>
3031
#include <utility>
3132
#include <functional>
@@ -118,7 +119,7 @@ struct is_valid_inplace_dst : std::true_type
118119
template<
119120
class Signature,
120121
size_t Capacity = inplace_function_detail::InplaceFunctionDefaultCapacity,
121-
size_t Alignment = alignof(modm::aligned_storage_t<Capacity>)
122+
size_t Alignment = default_storage_alignment<Capacity>
122123
>
123124
class inplace_function; // unspecified
124125

@@ -140,7 +141,6 @@ template<
140141
>
141142
class inplace_function<R(Args...), Capacity, Alignment>
142143
{
143-
using storage_t = modm::aligned_storage_t<Capacity, Alignment>;
144144
using vtable_t = inplace_function_detail::vtable<R, Args...>;
145145
using vtable_ptr_t = const vtable_t*;
146146

@@ -275,7 +275,7 @@ class inplace_function<R(Args...), Capacity, Alignment>
275275
{
276276
if (this == std::addressof(other)) return;
277277

278-
storage_t tmp;
278+
alignas(Alignment) std::byte tmp[Capacity];
279279
vtable_ptr_->relocate_ptr(
280280
std::addressof(tmp),
281281
std::addressof(storage_)
@@ -301,7 +301,7 @@ class inplace_function<R(Args...), Capacity, Alignment>
301301

302302
private:
303303
vtable_ptr_t vtable_ptr_;
304-
mutable storage_t storage_;
304+
alignas(Alignment) mutable std::byte storage_[Capacity];
305305

306306
inplace_function(
307307
vtable_ptr_t vtable_ptr,
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Copyright (c) 2026, Christopher Durand
3+
*
4+
* This file is part of the modm project.
5+
*
6+
* This Source Code Form is subject to the terms of the Mozilla Public
7+
* License, v. 2.0. If a copy of the MPL was not distributed with this
8+
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
9+
*/
10+
// ----------------------------------------------------------------------------
11+
12+
#include "inplace_function_test.hpp"
13+
#include <modm/utils/inplace_function.hpp>
14+
15+
void
16+
InplaceFunctionTest::testCall()
17+
{
18+
int value = 0;
19+
20+
modm::inplace_function<int(int), 8> func = [&value](int x) {
21+
value = x;
22+
return x * 2;
23+
};
24+
25+
TEST_ASSERT_EQUALS(func(42), 84);
26+
TEST_ASSERT_EQUALS(value, 42);
27+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
* Copyright (c) 2026, Christopher Durand
3+
*
4+
* This file is part of the modm project.
5+
*
6+
* This Source Code Form is subject to the terms of the Mozilla Public
7+
* License, v. 2.0. If a copy of the MPL was not distributed with this
8+
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
9+
*/
10+
// ----------------------------------------------------------------------------
11+
12+
#include <unittest/testsuite.hpp>
13+
14+
/// @ingroup modm_test_test_utils
15+
class InplaceFunctionTest : public unittest::TestSuite
16+
{
17+
public:
18+
void
19+
testCall();
20+
};

0 commit comments

Comments
 (0)