-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathtype_traits.cpp
More file actions
296 lines (247 loc) · 9.9 KB
/
type_traits.cpp
File metadata and controls
296 lines (247 loc) · 9.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
#include <stdx/ct_conversions.hpp>
#include <stdx/type_traits.hpp>
#include <catch2/catch_test_macros.hpp>
#include <cstddef>
#include <type_traits>
#include <utility>
namespace {
template <typename> struct unary_t {};
template <typename...> struct variadic_t {};
template <typename T> struct derived_t : unary_t<T> {};
} // namespace
TEST_CASE("detect specializations", "[type_traits]") {
STATIC_REQUIRE(stdx::is_specialization_of_v<unary_t<int>, unary_t>);
STATIC_REQUIRE(stdx::is_type_specialization_of_v<unary_t<int>, unary_t>);
STATIC_REQUIRE(stdx::is_specialization_of<unary_t<int>, unary_t>());
STATIC_REQUIRE(not stdx::is_specialization_of_v<int, unary_t>);
STATIC_REQUIRE(not stdx::is_type_specialization_of_v<int, unary_t>);
STATIC_REQUIRE(not stdx::is_specialization_of<int, unary_t>());
STATIC_REQUIRE(stdx::is_specialization_of_v<variadic_t<>, variadic_t>);
STATIC_REQUIRE(stdx::is_type_specialization_of_v<variadic_t<>, variadic_t>);
STATIC_REQUIRE(stdx::is_specialization_of<variadic_t<>, variadic_t>());
STATIC_REQUIRE(not stdx::is_specialization_of_v<int, variadic_t>);
STATIC_REQUIRE(not stdx::is_type_specialization_of_v<int, variadic_t>);
STATIC_REQUIRE(not stdx::is_specialization_of<int, variadic_t>());
}
TEST_CASE("derived types are not specializations", "[type_traits]") {
STATIC_REQUIRE(not stdx::is_specialization_of_v<derived_t<int>, unary_t>);
STATIC_REQUIRE(
not stdx::is_type_specialization_of_v<derived_t<int>, unary_t>);
STATIC_REQUIRE(not stdx::is_specialization_of<derived_t<int>, unary_t>());
}
namespace {
template <auto> struct value_unary_t {};
template <auto...> struct value_variadic_t {};
template <auto V> struct value_derived_t : value_unary_t<V> {};
} // namespace
TEST_CASE("detect specializations (value templates)", "[type_traits]") {
STATIC_REQUIRE(
stdx::is_value_specialization_of_v<value_unary_t<0>, value_unary_t>);
STATIC_REQUIRE(
stdx::is_specialization_of<value_unary_t<0>, value_unary_t>());
STATIC_REQUIRE(not stdx::is_value_specialization_of_v<int, value_unary_t>);
STATIC_REQUIRE(not stdx::is_specialization_of<int, value_unary_t>());
STATIC_REQUIRE(stdx::is_value_specialization_of_v<value_variadic_t<>,
value_variadic_t>);
STATIC_REQUIRE(
stdx::is_specialization_of<value_variadic_t<>, value_variadic_t>());
STATIC_REQUIRE(
not stdx::is_value_specialization_of_v<int, value_variadic_t>);
STATIC_REQUIRE(not stdx::is_specialization_of<int, value_variadic_t>());
}
TEST_CASE("derived types are not specializations (value templates)",
"[type_traits]") {
STATIC_REQUIRE(not stdx::is_value_specialization_of_v<value_derived_t<0>,
value_unary_t>);
STATIC_REQUIRE(
not stdx::is_specialization_of<value_derived_t<0>, value_unary_t>());
}
namespace {
enum E1 {};
enum struct E2 {};
} // namespace
TEST_CASE("is_scoped_enum", "[type_traits]") {
STATIC_REQUIRE(not stdx::is_scoped_enum_v<int>);
STATIC_REQUIRE(not stdx::is_scoped_enum_v<E1>);
STATIC_REQUIRE(stdx::is_scoped_enum_v<E2>);
STATIC_REQUIRE(not stdx::is_scoped_enum<int>::value);
STATIC_REQUIRE(not stdx::is_scoped_enum<E1>::value);
STATIC_REQUIRE(stdx::is_scoped_enum<E2>::value);
}
TEST_CASE("type_identity", "[type_traits]") {
STATIC_REQUIRE(std::is_same_v<stdx::type_identity_t<void>, void>);
}
TEST_CASE("type_or_t", "[type_traits]") {
STATIC_REQUIRE(
std::is_same_v<stdx::type_or_t<std::is_void, void, int>, void>);
STATIC_REQUIRE(
std::is_same_v<stdx::type_or_t<std::is_void, int, float>, float>);
STATIC_REQUIRE(std::is_same_v<stdx::type_or_t<std::is_void, int>, void>);
}
namespace {
int value{};
struct add_value {
add_value() = default;
add_value(int init) { value = init; }
template <typename T> constexpr auto operator()() const -> void {
value += T::value;
}
template <auto T> constexpr auto operator()() const -> void { value += T; }
};
struct add_values {
template <typename... Ts> constexpr auto operator()() const {
return (0 + ... + Ts::value);
}
template <auto... Ts> constexpr auto operator()() const {
return (0 + ... + Ts);
}
};
} // namespace
TEST_CASE("template_for_each with type list", "[type_traits]") {
value = 0;
using L = stdx::type_list<std::integral_constant<int, 1>,
std::integral_constant<int, 2>>;
stdx::template_for_each<L>(add_value{});
CHECK(value == 3);
}
TEST_CASE("template_for_each with empty type list", "[type_traits]") {
using L = stdx::type_list<>;
stdx::template_for_each<L>(add_value{17});
CHECK(value == 17);
}
TEST_CASE("template_for_each with value list", "[type_traits]") {
using L = stdx::value_list<1, 2>;
stdx::template_for_each<L>(add_value{0});
CHECK(value == 3);
}
TEST_CASE("template_for_each with empty value list", "[type_traits]") {
using L = stdx::value_list<>;
stdx::template_for_each<L>(add_value{17});
CHECK(value == 17);
}
TEST_CASE("template_for_each with index sequence", "[type_traits]") {
using L = std::make_index_sequence<3>;
stdx::template_for_each<L>(add_value{0});
CHECK(value == 3);
}
TEST_CASE("apply_sequence with type list", "[type_traits]") {
using L = stdx::type_list<std::integral_constant<int, 1>,
std::integral_constant<int, 2>>;
CHECK(stdx::apply_sequence<L>(add_values{}) == 3);
}
TEST_CASE("apply_sequence with value list", "[type_traits]") {
using L = stdx::value_list<1, 2>;
CHECK(stdx::apply_sequence<L>(add_values{}) == 3);
}
TEST_CASE("apply_sequence with index sequence", "[type_traits]") {
using L = std::make_index_sequence<3>;
CHECK(stdx::apply_sequence<L>(add_values{}) == 3);
STATIC_CHECK(std::is_same_v<decltype(stdx::apply_sequence<L>(add_values{})),
std::size_t>);
}
TEST_CASE("is_same_unqualified", "[type_traits]") {
STATIC_REQUIRE(stdx::is_same_unqualified_v<int, int>);
STATIC_REQUIRE(not stdx::is_same_unqualified_v<int, void>);
STATIC_REQUIRE(stdx::is_same_unqualified_v<int, int &>);
STATIC_REQUIRE(stdx::is_same_unqualified_v<int, int const &>);
STATIC_REQUIRE(stdx::is_same_unqualified_v<int, int &&>);
STATIC_REQUIRE(stdx::is_same_unqualified_v<int, int const &&>);
STATIC_REQUIRE(stdx::is_same_unqualified_v<int &, int>);
STATIC_REQUIRE(stdx::is_same_unqualified_v<int const &, int>);
STATIC_REQUIRE(stdx::is_same_unqualified_v<int &&, int>);
STATIC_REQUIRE(stdx::is_same_unqualified_v<int const &&, int>);
}
// for a taxonomy of structural types below, see
// https://en.cppreference.com/w/cpp/language/template_parameters#Non-type_template_parameter
namespace structural {
struct base_t {
int x;
auto f() { return 42; }
};
using pmd_t = decltype(&base_t::x);
using pmf_t = decltype(&base_t::f);
struct derived_t : base_t {
constexpr derived_t(int _x, int _y) : base_t{_x}, y{_y} {}
int y;
};
struct class_t : base_t {
constexpr class_t(int _x, int _y) : base_t{_x}, d{_x, _y}, y{_y} {}
derived_t d;
int y;
};
enum struct enum_t {};
constexpr auto l = [](int) { return 42; };
using lambda_t = decltype(l);
using union_t = union {
int x;
int y;
};
} // namespace structural
TEST_CASE("structural types", "[type_traits]") {
STATIC_REQUIRE(stdx::is_structural_v<int &>);
STATIC_REQUIRE(stdx::is_structural_v<int>);
STATIC_REQUIRE(stdx::is_structural_v<int *>);
STATIC_REQUIRE(stdx::is_structural_v<structural::pmd_t>);
STATIC_REQUIRE(stdx::is_structural_v<structural::pmf_t>);
STATIC_REQUIRE(stdx::is_structural_v<structural::enum_t>);
STATIC_REQUIRE(stdx::is_structural_v<std::nullptr_t>);
#if __cpp_nontype_template_args >= 201911L
STATIC_REQUIRE(stdx::is_structural_v<structural::base_t>);
STATIC_REQUIRE(stdx::is_structural_v<structural::derived_t>);
STATIC_REQUIRE(stdx::is_structural_v<structural::class_t>);
STATIC_REQUIRE(stdx::is_structural_v<structural::lambda_t>);
STATIC_REQUIRE(stdx::is_structural_v<structural::union_t>);
STATIC_REQUIRE(stdx::is_structural_v<float>);
#endif
}
namespace non_structural {
struct S {
~S() {} // nontrivial destructor
};
} // namespace non_structural
TEST_CASE("non-structural types", "[type_traits]") {
STATIC_REQUIRE(not stdx::is_structural_v<non_structural::S>);
}
namespace {
template <typename...> struct long_type_name {};
using A = long_type_name<int, int, int, int, int, int, int, int>;
using B = long_type_name<A, A, A, A, A, A, A, A>;
using C = long_type_name<B, B, B, B, B, B, B, B>;
} // namespace
TEST_CASE("type shrinkage (by type)", "[type_traits]") {
using X = stdx::shrink_t<C>;
#if __cplusplus >= 202002L
STATIC_CHECK(stdx::type_as_string<X>().size() <
stdx::type_as_string<C>().size());
#endif
STATIC_CHECK(std::is_same_v<stdx::expand_t<X>, C>);
}
TEST_CASE("type shrinkage (by value)", "[type_traits]") {
auto c = C{};
auto x = stdx::shrink(c);
auto y = stdx::expand(x);
#if __cplusplus >= 202002L
STATIC_CHECK(stdx::type_as_string<decltype(x)>().size() <
stdx::type_as_string<C>().size());
#endif
STATIC_CHECK(std::is_same_v<decltype(y), C>);
}
TEST_CASE("nth type in pack", "[type_traits]") {
STATIC_REQUIRE(
std::is_same_v<stdx::nth_t<2, bool, char, float, int>, float>);
}
#if __cplusplus >= 202002L
TEST_CASE("nth value in pack", "[type_traits]") {
STATIC_REQUIRE(stdx::nth_v<2, 0, true, 'b', 3> == 'b');
}
#endif
TEST_CASE("is_complete_v", "[type_traits]") {
struct incomplete;
STATIC_REQUIRE(stdx::is_complete_v<int>);
STATIC_REQUIRE(not stdx::is_complete_v<incomplete>);
}
TEST_CASE("is_same_template_v", "[type_traits]") {
STATIC_REQUIRE(stdx::is_same_template_v<unary_t<int>, unary_t<void>>);
STATIC_REQUIRE(
not stdx::is_same_template_v<unary_t<int>, variadic_t<void>>);
}