Arene Base
Fundamental Utilities For Safety Critical C++
Loading...
Searching...
No Matches
make_signed.hpp
Go to the documentation of this file.
1// Copyright 2024, 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_MAKE_SIGNED_HPP_
6#define INCLUDE_GUARD_ARENE_BASE_STDLIB_INCLUDE_STDLIB_DETAIL_MAKE_SIGNED_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
27
28namespace std {
29
30// parasoft-begin-suppress AUTOSAR-A2_7_3-a "False positive: all declarations
31// and typedefs *are* preceded by a comment with @brief"
32
34/// @brief Internal class providing @c std::make_signed
35/// @tparam Type The type to convert to a signed equivalent
36// parasoft-begin-suppress AUTOSAR-A11_0_2 "False positive: Not used as a base class here"
37template <typename Type, typename = arene::base::constraints<>>
38struct make_signed {};
39// parasoft-end-suppress AUTOSAR-A11_0_2
40
41/// @brief Internal class providing @c std::make_signed for integral types that are already signed
42/// @tparam Type The type to convert to a signed equivalent
43template <typename Type>
45 Type,
46 arene::base::constraints<
47 enable_if_t<is_integral_v<Type>>,
48 enable_if_t<is_signed_v<Type>>,
49 enable_if_t<!is_same_v<Type, bool>>,
50 enable_if_t<!is_const_v<Type>>,
51 enable_if_t<!is_volatile_v<Type>>>> {
52 /// @brief The resulting type
53 using type = Type;
54};
55
56/// @brief Internal class providing @c std::make_signed for integral types or enumeration types that are @c const
57/// qualified
58/// @tparam Type The type to convert to a signed equivalent
59template <typename Type>
61 Type,
62 arene::base::constraints<
63 enable_if_t<is_integral_v<Type> || is_enum_v<Type>>,
64 enable_if_t<is_const_v<Type>>,
65 enable_if_t<!is_volatile_v<Type>>>> {
66 /// @brief The resulting type
68};
69
70/// @brief Internal class providing @c std::make_signed for integral types or enumeration types that are @c volatile
71/// qualified
72/// @tparam Type The type to convert to a signed equivalent
73template <typename Type>
75 Type,
76 arene::base::constraints<
77 enable_if_t<is_integral_v<Type> || is_enum_v<Type>>,
78 enable_if_t<is_volatile_v<Type>>,
79 enable_if_t<!is_const_v<Type>>>> {
80 /// @brief The resulting type
82};
83
84/// @brief Internal class providing @c std::make_signed for integral types or enumeration types that are @c const
85/// and @c volatile qualified
86/// @tparam Type The type to convert to a signed equivalent
87template <typename Type>
89 Type,
90 arene::base::constraints<
91 enable_if_t<is_integral_v<Type> || is_enum_v<Type>>,
92 enable_if_t<is_const_v<Type>>,
93 enable_if_t<is_volatile_v<Type>>>> {
94 /// @brief The resulting type
96};
97
98// parasoft-begin-suppress AUTOSAR-A3_9_1-b-2 "The built-in types must be handled explicitly here for correctness"
99/// @brief Internal class providing @c std::make_signed for @c unsigned @c char
100template <>
101struct make_signed<unsigned char> {
102 /// @brief The resulting type
103 using type = signed char;
104};
105
106/// @brief Internal class providing @c std::make_signed for @c unsigned @c short
107template <>
108// NOLINTNEXTLINE(google-runtime-int)
109struct make_signed<unsigned short> {
110 /// @brief The resulting type
111 // NOLINTNEXTLINE(google-runtime-int)
112 using type = short;
113};
114
115/// @brief Internal class providing @c std::make_signed for @c unsigned @c int
116template <>
117struct make_signed<unsigned int> {
118 /// @brief The resulting type
119 using type = int;
120};
121
122/// @brief Internal class providing @c std::make_signed for @c long
123template <>
124// NOLINTNEXTLINE(google-runtime-int)
125struct make_signed<unsigned long> {
126 /// @brief The resulting type
127 // NOLINTNEXTLINE(google-runtime-int)
128 using type = long;
129};
130
131/// @brief Internal class providing @c std::make_signed for @c unsigned @c long @c long
132template <>
133// NOLINTNEXTLINE(google-runtime-int)
134struct make_signed<unsigned long long> {
135 /// @brief The resulting type
136 // NOLINTNEXTLINE(google-runtime-int)
137 using type = long long;
138};
139
140/// @brief Internal class providing @c std::make_signed for @c char
141template <>
142struct make_signed<char> {
143 /// @brief The resulting type
144 // NOLINTNEXTLINE(google-runtime-int)
145 using type = signed char;
146};
147
148/// @brief Internal class providing a signed type with a specified size
149/// @tparam Size the size to check
150template <size_t Size, typename = arene::base::constraints<>>
151struct sized_signed {};
152
153/// @brief Internal class providing a signed type with the size of @c char
154/// @tparam Size the size to check
155template <size_t Size>
156struct sized_signed<Size, arene::base::constraints<enable_if_t<Size == sizeof(char)>>> {
157 /// @brief The resulting type
158 using type = signed char;
159};
160
161/// @brief Internal class providing a signed type with the size of @c short
162/// @tparam Size the size to check
163template <size_t Size>
164// NOLINTNEXTLINE(google-runtime-int)
165struct sized_signed<Size, arene::base::constraints<enable_if_t<Size == sizeof(short)>>> {
166 /// @brief The resulting type
167 // NOLINTNEXTLINE(google-runtime-int)
168 using type = short;
169};
170
171/// @brief Internal class providing a signed type with the size of @c int
172/// @tparam Size the size to check
173template <size_t Size>
175 Size,
176 // NOLINTNEXTLINE(google-runtime-int)
177 arene::base::constraints<enable_if_t<(Size > sizeof(short))>, enable_if_t<Size == sizeof(int)>>> {
178 /// @brief The resulting type
179 using type = int;
180};
181
182/// @brief Internal class providing a signed type with the size of @c long
183/// @tparam Size the size to check
184template <size_t Size>
186 Size,
187 // NOLINTNEXTLINE(google-runtime-int)
188 arene::base::constraints<enable_if_t<(Size > sizeof(int))>, enable_if_t<Size == sizeof(long)>>> {
189 /// @brief The resulting type
190 // NOLINTNEXTLINE(google-runtime-int)
191 using type = long;
192};
193
194/// @brief Internal class providing a signed type with the size of @c long @c long
195/// @tparam Size the size to check
196template <size_t Size>
198 Size,
199 // NOLINTNEXTLINE(google-runtime-int)
200 arene::base::constraints<enable_if_t<(Size > sizeof(long))>, enable_if_t<Size == sizeof(long long)>>> {
201 /// @brief The resulting type
202 // NOLINTNEXTLINE(google-runtime-int)
203 using type = long long;
204};
205// parasoft-end-suppress AUTOSAR-A3_9_1-b-2
206
207// parasoft-begin-suppress AUTOSAR-A2_13_3-a-2 "wchar_t must be used here to ensure correctness"
208/// @brief Internal class providing a signed type for @c wchar_t
209template <>
210struct make_signed<wchar_t> {
211 /// @brief The resulting type
212 // NOLINTNEXTLINE(google-runtime-int)
213 using type = typename sized_signed<sizeof(wchar_t)>::type;
214};
215// parasoft-end-suppress AUTOSAR-A2_13_3-a-2
216
217/// @brief Internal class providing a signed type for @c char16_t
218template <>
219struct make_signed<char16_t> {
220 /// @brief The resulting type
221 using type = typename sized_signed<sizeof(char16_t)>::type;
222};
223
224/// @brief Internal class providing a signed type for @c char32_t
225template <>
226struct make_signed<char32_t> {
227 /// @brief The resulting type
228 using type = typename sized_signed<sizeof(char32_t)>::type;
229};
230
231/// @brief Internal class providing @c std::make_signed for integral types that are already signed
232/// @tparam Type The type to convert to a signed equivalent
233template <typename Type>
235 Type,
236 arene::base::
237 constraints<enable_if_t<is_enum_v<Type>>, enable_if_t<!is_const_v<Type>>, enable_if_t<!is_volatile_v<Type>>>> {
238 /// @brief The resulting type
239 using type = typename sized_signed<sizeof(Type)>::type;
240};
241
242} // namespace make_signed_detail
243
244/// @brief A type trait to provide the equivalent signed type for integral and enumeration types other than @c bool
245/// @tparam Type The type to get the equivalent of
246template <typename Type>
248
249/// @brief A type alias for the equivalent signed type for integral and enumeration types other than @c bool
250/// @tparam Type The type to get the equivalent of
251template <typename Type>
253
254// parasoft-end-suppress AUTOSAR-A2_7_3-a
255
256} // namespace std
257
258#endif // INCLUDE_GUARD_ARENE_BASE_STDLIB_INCLUDE_STDLIB_DETAIL_MAKE_SIGNED_HPP_
Public export header for the constraints subpackage.
Definition accumulate.hpp:36
Definition make_signed.hpp:33
STL namespace.
typename make_signed< Type >::type make_signed_t
A type alias for the equivalent signed type for integral and enumeration types other than bool.
Definition make_signed.hpp:252
typename add_volatile< Type >::type add_volatile_t
Add volatile to the provided type. An alias for the modified type.
Definition add_volatile.hpp:29
typename enable_if< Condition, Type >::type enable_if_t
Type alias to support constraining templates. Is an alias for the specified type if the Condition is ...
Definition enable_if.hpp:41
typename add_const< Type >::type add_const_t
Add const to the provided type. An alias for the modified type.
Definition add_const.hpp:27
make_signed_detail::make_signed< Type > make_signed
A type trait to provide the equivalent signed type for integral and enumeration types other than bool...
Definition make_signed.hpp:247
typename sized_signed< sizeof(char16_t)>::type type
The resulting type.
Definition make_signed.hpp:221
typename sized_signed< sizeof(char32_t)>::type type
The resulting type.
Definition make_signed.hpp:228
signed char type
The resulting type.
Definition make_signed.hpp:145
signed char type
The resulting type.
Definition make_signed.hpp:103
int type
The resulting type.
Definition make_signed.hpp:119
long type
The resulting type.
Definition make_signed.hpp:128
long long type
The resulting type.
Definition make_signed.hpp:137
short type
The resulting type.
Definition make_signed.hpp:112
typename sized_signed< sizeof(wchar_t)>::type type
The resulting type.
Definition make_signed.hpp:213
Internal class providing std::make_signed.
Definition make_signed.hpp:38
Internal class providing a signed type with a specified size.
Definition make_signed.hpp:151