tetl 0.1.0
Embedded Template Library
Loading...
Searching...
No Matches
aligned_union.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_UNION_HPP
5#define TETL_TYPE_TRAITS_ALIGNED_UNION_HPP
6
7#include <etl/_cstddef/size_t.hpp>
8
9namespace etl {
10
11namespace detail {
12template <typename T>
13[[nodiscard]] constexpr auto vmax(T val) -> T
14{
15 return val;
16}
17
18template <typename T0, typename T1, typename... Ts>
19[[nodiscard]] constexpr auto vmax(T0 val1, T1 val2, Ts... vs) -> T0
20{
21 return (val1 > val2) ? vmax(val1, vs...) : vmax(val2, vs...);
22}
23} // namespace detail
24
25/// \brief Provides the nested type type, which is a trivial standard-layout
26/// type of a size and alignment suitable for use as uninitialized storage for
27/// an object of any of the types listed in Types. The size of the storage is at
28/// least Len. aligned_union also determines the strictest (largest) alignment
29/// requirement among all Types and makes it available as the constant
30/// alignment_value. If sizeof...(Types) == 0 or if any of the types in Types is
31/// not a complete object type, the behavior is undefined. It is
32/// implementation-defined whether any extended alignment is supported. The
33/// behavior of a program that adds specializations for aligned_union is
34/// undefined.
35template <size_t Len, typename... Types>
37 static constexpr size_t alignment_value = detail::vmax(alignof(Types)...);
38
39 struct type {
40 alignas(alignment_value) char storage[detail::vmax(Len, sizeof(Types)...)];
41 };
42};
43
44template <size_t Len, typename... Types>
45using aligned_union_t = typename aligned_union<Len, Types...>::type;
46
47} // namespace etl
48
49#endif // TETL_TYPE_TRAITS_ALIGNED_UNION_HPP
Definition adjacent_find.hpp:9
Definition aligned_union.hpp:39
char storage[detail::vmax(Len, sizeof(Types)...)]
Definition aligned_union.hpp:40
Provides the nested type type, which is a trivial standard-layout type of a size and alignment suitab...
Definition aligned_union.hpp:36
static constexpr size_t alignment_value
Definition aligned_union.hpp:37