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.
28///
29/// The size of the storage is at least Len. aligned_union also determines the
30/// strictest (largest) alignment requirement among all Types and makes it
31/// available as the constant alignment_value. If sizeof...(Types) == 0 or if
32/// any of the types in Types is not a complete object type, the behavior is undefined.
33///
34/// It is implementation-defined whether any extended alignment is supported. The
35/// behavior of a program that adds specializations for aligned_union is
36/// undefined.
37///
38/// \ingroup type_traits
39template <size_t Len, typename... Types>
41 static constexpr size_t alignment_value = detail::vmax(alignof(Types)...);
42
43 struct type {
44 alignas(alignment_value) char storage[detail::vmax(Len, sizeof(Types)...)];
45 };
46};
47
48/// \relates aligned_union
49/// \ingroup type_traits
50template <size_t Len, typename... Types>
51using aligned_union_t = typename aligned_union<Len, Types...>::type;
52
53} // namespace etl
54
55#endif // TETL_TYPE_TRAITS_ALIGNED_UNION_HPP
Definition adjacent_find.hpp:9
Definition aligned_union.hpp:43
char storage[detail::vmax(Len, sizeof(Types)...)]
Definition aligned_union.hpp:44
Provides the nested type type, which is a trivial standard-layout type of a size and alignment suitab...
Definition aligned_union.hpp:40
static constexpr size_t alignment_value
Definition aligned_union.hpp:41