tetl 0.1.0
Embedded Template Library
Loading...
Searching...
No Matches
aligned_storage.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: BSL-1.0
2// SPDX-FileCopyrightText: Copyright (C) 2019 Tobias Hienzsch
3
4#ifndef TETL_TYPE_TRAITS_ALIGNED_STORAGE_HPP
5#define TETL_TYPE_TRAITS_ALIGNED_STORAGE_HPP
6
7#include <etl/_cstddef/max_align_t.hpp>
8#include <etl/_cstddef/size_t.hpp>
9#include <etl/_type_traits/conditional.hpp>
10
11namespace etl {
12
13namespace detail {
14// With this change, types smaller then 16 bytes are aligned to their size. This
15// equals the behavoir from libc++ and MSVC-STL. Copied from
16// https://github.com/WG21-SG14/SG14/commit/98baf1aeab
17// https://gcc.gnu.org/bugzilla/show_bug.cgi?id=61458#c10
18template <etl::size_t Cap>
19union aligned_storage_impl {
20 template <typename T>
21 using maybe = conditional_t<(Cap >= sizeof(T)), T, char>;
22
23 struct double1 {
24 double a;
25 };
26
27 struct double4 {
28 double a[4];
29 };
30
31 char real_data[Cap];
32 maybe<int> a;
33 maybe<long> b;
34 maybe<long long> c;
35 maybe<void*> d;
36 maybe<void (*)()> e;
37 maybe<double1> f;
38 maybe<double4> g;
39 maybe<long double> h;
40};
41} // namespace detail
42
43/// \brief Provides the nested type type, which is a trivial standard-layout
44/// type suitable for use as uninitialized storage for any object whose size is
45/// at most Len and whose alignment requirement is a divisor of Align.
46/// The default value of Align is the most stringent (the largest)
47/// alignment requirement for any object whose size is at most Len. If the
48/// default value is not used, Align must be the value of alignof(T) for some
49/// type T, or the behavior is undefined.
50template <size_t Len, size_t Align = alignof(detail::aligned_storage_impl<Len>)>
52 struct type {
53 alignas(Align) unsigned char data[Len];
54 };
55};
56
57template <size_t Len, size_t Align = alignof(detail::aligned_storage_impl<Len>)>
58using aligned_storage_t = typename aligned_storage<Len, Align>::type;
59
60} // namespace etl
61
62#endif // TETL_TYPE_TRAITS_ALIGNED_STORAGE_HPP
Definition adjacent_find.hpp:9
Definition aligned_storage.hpp:52
unsigned char data[Len]
Definition aligned_storage.hpp:53
Provides the nested type type, which is a trivial standard-layout type suitable for use as uninitiali...
Definition aligned_storage.hpp:51