Arene Base
Fundamental Utilities For Safety Critical C++
Loading...
Searching...
No Matches
common_type.hpp
Go to the documentation of this file.
1// Copyright 2026, Toyota Motor Corporation
2//
3// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4
5#ifndef INCLUDE_GUARD_ARENE_BASE_STDLIB_INCLUDE_STDLIB_DETAIL_COMMON_TYPE_HPP_
6#define INCLUDE_GUARD_ARENE_BASE_STDLIB_INCLUDE_STDLIB_DETAIL_COMMON_TYPE_HPP_
7
8// parasoft-begin-suppress CERT_CPP-DCL58-a-2 "Part of a standard library implementation"
9// parasoft-begin-suppress AUTOSAR-A17_6_1-a-2 "Part of a standard library implementation"
10
11// IWYU pragma: private, include <type_traits>
12// IWYU pragma: friend "stdlib_detail/.*"
13
14#include "arene/base/constraints.hpp"
15#include "stdlib/include/stdlib_detail/conditional.hpp"
16#include "stdlib/include/stdlib_detail/decay.hpp"
17#include "stdlib/include/stdlib_detail/declval.hpp"
18#include "stdlib/include/stdlib_detail/enable_if.hpp"
19#include "stdlib/include/stdlib_detail/is_same.hpp"
20
21namespace std {
22
24
25// parasoft-begin-suppress AUTOSAR-A2_7_3 "False positive: Everything in this file has a comment that contains @brief"
26
27/// @brief Helper type that determines the type of a conditional expression between @c T1 and @c T2
28/// @tparam T1 First type
29/// @tparam T2 Second type
30template <class T1, class T2>
31using conditional_result_t = decltype(false ? declval<T1>() : declval<T2>());
32
33/// @brief Primary template for decaying conditional result types
34/// @tparam T1 First type
35/// @tparam T2 Second type
36/// @tparam Empty SFINAE constraints parameter
37template <class T1, class T2, class = arene::base::constraints<>>
39
40/// @brief Specialization that provides the decayed type of a conditional expression
41/// @tparam T1 First type
42/// @tparam T2 Second type
43template <class T1, class T2>
48
49/// @brief Helper alias template for decayed_conditional_result
50/// @tparam T1 First type
51/// @tparam T2 Second type
52template <class T1, class T2>
54
55/// @brief Helper variable template that checks if both types are already decayed
56/// @tparam T1 First type
57/// @tparam T2 Second type
58template <class T1, class T2>
59extern constexpr bool are_already_decayed_v = is_same_v<T1, decay_t<T1>> && is_same_v<T2, decay_t<T2>>;
60
61} // namespace common_type_detail
62
63/// @brief Primary template for determining the common type of a set of types
64///
65/// The primary template is the base case for the empty parameter pack, which defines no member alias named @c type.
66/// Note: This differs from the original published C++14 specification, which was not SFINAE friendly, to be compliant
67/// with LWG issues 2408 and 2465.
68///
69/// @tparam Empty parameter pack
70template <class...>
71class common_type {};
72
73/// @brief Specialization for a single type
74///
75/// For a single type T, the common type is defined as the common type of T with itself.
76///
77/// @tparam T The single type
78template <class T>
79class common_type<T> : public common_type<T, T> {};
80
81/// @brief Specialization for two types
82///
83/// Determines the common type of two types @c T1 and @c T2. If both types are already
84/// decayed, computes the decayed type of the conditional expression <c> false ? T1 : T2 </c>
85/// Otherwise, determines the common type of the decayed forms of @c T1 and @c T2.
86///
87/// @tparam T1 First type
88/// @tparam T2 Second type
89template <typename T1, typename T2>
95
96/// @brief Helper alias template for common_type
97/// @tparam Ts Types to find common type for
98template <class... Ts>
99using common_type_t = typename common_type<Ts...>::type;
100
101namespace common_type_detail {
102
103/// @brief Primary template for computing common type of 3 or more types
104/// @tparam Constraints SFINAE constraints parameter
105/// @tparam T1 First type
106/// @tparam T2 Second type
107/// @tparam Ts Remaining types
108template <class Constraints, typename T1, typename T2, typename... Ts>
110
111/// @brief Specialization for computing common type of 3 or more types
112/// @tparam T1 First type
113/// @tparam T2 Second type
114/// @tparam Ts Remaining types
115template <typename T1, typename T2, typename... Ts>
118
119} // namespace common_type_detail
120
121/// @brief Specialization for three or more types
122///
123/// This specialization handles the case of three or more types by recursively
124/// finding the common type. It first finds the common type of the first two types,
125/// then finds the common type of that result with the remaining types.
126///
127/// @tparam T1 First type
128/// @tparam T2 Second type
129/// @tparam Ts Remaining types
130template <typename T1, typename T2, typename... Ts>
131class common_type<T1, T2, Ts...>
132 : public common_type_detail::common_type_3_or_more<arene::base::constraints<>, T1, T2, Ts...> {};
133
134} // namespace std
135
136#endif // INCLUDE_GUARD_ARENE_BASE_STDLIB_INCLUDE_STDLIB_DETAIL_COMMON_TYPE_HPP_
Primary template for computing common type of 3 or more types.
Definition common_type.hpp:109
Primary template for decaying conditional result types.
Definition common_type.hpp:38
Primary template for determining the common type of a set of types.
Definition common_type.hpp:71
Definition common_type.hpp:23
constexpr bool are_already_decayed_v
Helper variable template that checks if both types are already decayed.
constexpr auto operator()(::arene::base::result< void, E > const &value) const noexcept(noexcept(hash< E >{}(std::declval< E const & >()))) -> std::size_t
Calculate the hash of a result.
Definition result.hpp:1827