-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathfunction_traits.cpp
More file actions
376 lines (323 loc) · 12.9 KB
/
function_traits.cpp
File metadata and controls
376 lines (323 loc) · 12.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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
#include <stdx/function_traits.hpp>
#include <stdx/priority.hpp>
#include <catch2/catch_test_macros.hpp>
#include <tuple>
#include <type_traits>
namespace {
[[maybe_unused]] auto func_no_args() -> void {}
[[maybe_unused]] auto func_one_arg(int) -> int { return 0; }
[[maybe_unused]] auto func_ref_arg(int &) -> void {}
} // namespace
TEST_CASE("function return type", "[function_traits]") {
STATIC_REQUIRE(std::is_void_v<typename stdx::function_traits<
decltype(func_no_args)>::return_type>);
STATIC_REQUIRE(std::is_void_v<stdx::return_t<decltype(func_no_args)>>);
STATIC_REQUIRE(
std::is_same_v<
typename stdx::function_traits<decltype(func_one_arg)>::return_type,
int>);
STATIC_REQUIRE(std::is_same_v<stdx::return_t<decltype(func_one_arg)>, int>);
}
TEST_CASE("function pointer return type", "[function_traits]") {
STATIC_REQUIRE(
std::is_void_v<typename stdx::function_traits<
std::add_pointer_t<decltype(func_no_args)>>::return_type>);
STATIC_REQUIRE(std::is_void_v<
stdx::return_t<std::add_pointer_t<decltype(func_no_args)>>>);
STATIC_REQUIRE(
std::is_same_v<typename stdx::function_traits<std::add_pointer_t<
decltype(func_one_arg)>>::return_type,
int>);
STATIC_REQUIRE(
std::is_same_v<
stdx::return_t<std::add_pointer_t<decltype(func_one_arg)>>, int>);
}
TEST_CASE("qualified function pointer return type", "[function_traits]") {
STATIC_REQUIRE(
std::is_void_v<
stdx::return_t<std::add_pointer_t<decltype(func_no_args)> const>>);
STATIC_REQUIRE(std::is_void_v<stdx::return_t<
std::add_pointer_t<decltype(func_no_args)> volatile>>);
STATIC_REQUIRE(
std::is_void_v<stdx::return_t<
std::add_pointer_t<decltype(func_no_args)> const volatile>>);
}
TEST_CASE("lambda return type", "[function_traits]") {
[[maybe_unused]] auto const x = []() {};
[[maybe_unused]] auto const y = []() mutable {};
STATIC_REQUIRE(std::is_void_v<stdx::return_t<decltype(x)>>);
STATIC_REQUIRE(std::is_void_v<stdx::return_t<decltype(y)>>);
}
TEST_CASE("function args", "[function_traits]") {
STATIC_REQUIRE(
std::is_same_v<typename stdx::function_traits<
decltype(func_no_args)>::template args<std::tuple>,
std::tuple<>>);
STATIC_REQUIRE(
std::is_same_v<stdx::args_t<decltype(func_no_args), std::tuple>,
std::tuple<>>);
STATIC_REQUIRE(
std::is_same_v<typename stdx::function_traits<
decltype(func_one_arg)>::template args<std::tuple>,
std::tuple<int>>);
STATIC_REQUIRE(
std::is_same_v<stdx::args_t<decltype(func_one_arg), std::tuple>,
std::tuple<int>>);
}
TEST_CASE("function decayed args", "[function_traits]") {
STATIC_REQUIRE(
std::is_same_v<typename stdx::function_traits<decltype(func_ref_arg)>::
template decayed_args<std::tuple>,
std::tuple<int>>);
STATIC_REQUIRE(
std::is_same_v<stdx::decayed_args_t<decltype(func_ref_arg), std::tuple>,
std::tuple<int>>);
}
TEST_CASE("function nth arg", "[function_traits]") {
STATIC_REQUIRE(
std::is_same_v<typename stdx::function_traits<
decltype(func_one_arg)>::template nth_arg<0>,
int>);
STATIC_REQUIRE(
std::is_same_v<stdx::nth_arg_t<decltype(func_one_arg), 0>, int>);
}
TEST_CASE("function decayed nth arg", "[function_traits]") {
STATIC_REQUIRE(
std::is_same_v<typename stdx::function_traits<
decltype(func_ref_arg)>::template decayed_nth_arg<0>,
int>);
STATIC_REQUIRE(
std::is_same_v<stdx::decayed_nth_arg_t<decltype(func_ref_arg), 0>,
int>);
}
TEST_CASE("lambda args", "[function_traits]") {
[[maybe_unused]] auto const x = [](int) {};
[[maybe_unused]] auto const y = [](int) mutable {};
STATIC_REQUIRE(std::is_same_v<typename stdx::function_traits<
decltype(x)>::template args<std::tuple>,
std::tuple<int>>);
STATIC_REQUIRE(
std::is_same_v<stdx::args_t<decltype(x), std::tuple>, std::tuple<int>>);
STATIC_REQUIRE(std::is_same_v<typename stdx::function_traits<
decltype(y)>::template args<std::tuple>,
std::tuple<int>>);
STATIC_REQUIRE(
std::is_same_v<stdx::args_t<decltype(y), std::tuple>, std::tuple<int>>);
}
TEST_CASE("lambda decayed args", "[function_traits]") {
[[maybe_unused]] auto const x = [](int &) {};
[[maybe_unused]] auto const y = [](int &) mutable {};
STATIC_REQUIRE(
std::is_same_v<typename stdx::function_traits<
decltype(x)>::template decayed_args<std::tuple>,
std::tuple<int>>);
STATIC_REQUIRE(std::is_same_v<stdx::decayed_args_t<decltype(x), std::tuple>,
std::tuple<int>>);
STATIC_REQUIRE(
std::is_same_v<typename stdx::function_traits<
decltype(y)>::template decayed_args<std::tuple>,
std::tuple<int>>);
STATIC_REQUIRE(std::is_same_v<stdx::decayed_args_t<decltype(y), std::tuple>,
std::tuple<int>>);
}
TEST_CASE("lambda nth arg", "[function_traits]") {
[[maybe_unused]] auto const x = [](int) {};
STATIC_REQUIRE(
std::is_same_v<
typename stdx::function_traits<decltype(x)>::template nth_arg<0>,
int>);
STATIC_REQUIRE(std::is_same_v<stdx::nth_arg_t<decltype(x), 0>, int>);
}
TEST_CASE("lambda decayed nth arg", "[function_traits]") {
[[maybe_unused]] auto const x = [](int &) {};
STATIC_REQUIRE(std::is_same_v<typename stdx::function_traits<
decltype(x)>::template decayed_nth_arg<0>,
int>);
STATIC_REQUIRE(
std::is_same_v<stdx::decayed_nth_arg_t<decltype(x), 0>, int>);
}
TEST_CASE("function arity", "[function_traits]") {
STATIC_REQUIRE(
stdx::function_traits<decltype(func_no_args)>::arity::value == 0u);
STATIC_REQUIRE(
stdx::function_traits<decltype(func_one_arg)>::arity::value == 1u);
STATIC_REQUIRE(stdx::arity_t<decltype(func_no_args)>::value == 0u);
STATIC_REQUIRE(stdx::arity_t<decltype(func_one_arg)>::value == 1u);
STATIC_REQUIRE(stdx::arity_v<decltype(func_no_args)> == 0u);
STATIC_REQUIRE(stdx::arity_v<decltype(func_one_arg)> == 1u);
}
TEST_CASE("lambda arity", "[function_traits]") {
[[maybe_unused]] auto const x = []() {};
[[maybe_unused]] auto const y = [](int) {};
STATIC_REQUIRE(stdx::function_traits<decltype(x)>::arity::value == 0u);
STATIC_REQUIRE(stdx::function_traits<decltype(y)>::arity::value == 1u);
STATIC_REQUIRE(stdx::arity_t<decltype(x)>::value == 0u);
STATIC_REQUIRE(stdx::arity_t<decltype(y)>::value == 1u);
STATIC_REQUIRE(stdx::arity_v<decltype(x)> == 0u);
STATIC_REQUIRE(stdx::arity_v<decltype(y)> == 1u);
}
TEST_CASE("generic lambda arity", "[function_traits]") {
[[maybe_unused]] auto const x = [](auto) {};
[[maybe_unused]] auto const y = [](auto, auto) {};
STATIC_REQUIRE(stdx::arity_t<decltype(x)>::value == 1u);
STATIC_REQUIRE(stdx::arity_t<decltype(y)>::value == 2u);
STATIC_REQUIRE(stdx::arity_v<decltype(x)> == 1u);
STATIC_REQUIRE(stdx::arity_v<decltype(y)> == 2u);
}
namespace {
int called_1{};
int called_2{};
template <typename F>
constexpr auto call_f(F f, stdx::priority_t<1>) -> stdx::return_t<F> {
++called_1;
return f();
}
template <typename F> constexpr auto call_f(F f, stdx::priority_t<0>) -> void {
++called_2;
f(0);
}
} // namespace
TEST_CASE("SFINAE friendly", "[function_traits]") {
called_1 = 0;
called_2 = 0;
auto f1 = []() -> int { return 1; };
auto f2 = [](auto) -> void {};
call_f(f1, stdx::priority<1>);
CHECK(called_1 == 1);
CHECK(called_2 == 0);
call_f(f2, stdx::priority<1>);
CHECK(called_2 == 1);
}
namespace {
struct S {
[[maybe_unused]] auto f() -> void {}
};
struct S_C {
[[maybe_unused]] auto f() const -> void {}
};
struct S_V {
[[maybe_unused]] auto f() volatile -> void {}
};
struct S_CV {
[[maybe_unused]] auto f() const volatile -> void {}
};
struct S_LV {
[[maybe_unused]] auto f() & -> void {}
};
struct S_RV {
[[maybe_unused]] auto f() && -> void {}
};
struct S_CLV {
[[maybe_unused]] auto f() const & -> void {}
};
struct S_CRV {
[[maybe_unused]] auto f() const && -> void {}
};
struct S_VLV {
[[maybe_unused]] auto f() volatile & -> void {}
};
struct S_VRV {
[[maybe_unused]] auto f() volatile && -> void {}
};
struct S_CVLV {
[[maybe_unused]] auto f() const volatile & -> void {}
};
struct S_CVRV {
[[maybe_unused]] auto f() const volatile && -> void {}
};
} // namespace
TEST_CASE("member function return type", "[function_traits]") {
STATIC_REQUIRE(std::is_void_v<stdx::return_t<decltype(&S::f)>>);
STATIC_REQUIRE(std::is_void_v<stdx::return_t<decltype(&S_C::f)>>);
STATIC_REQUIRE(std::is_void_v<stdx::return_t<decltype(&S_V::f)>>);
STATIC_REQUIRE(std::is_void_v<stdx::return_t<decltype(&S_CV::f)>>);
}
TEST_CASE("member function arity", "[function_traits]") {
STATIC_REQUIRE(stdx::arity_v<decltype(&S::f)> == 0);
STATIC_REQUIRE(stdx::arity_v<decltype(&S_C::f)> == 0);
STATIC_REQUIRE(stdx::arity_v<decltype(&S_V::f)> == 0);
STATIC_REQUIRE(stdx::arity_v<decltype(&S_CV::f)> == 0);
}
TEST_CASE("object argument types (const/volatile)", "[function_traits]") {
STATIC_REQUIRE(std::is_void_v<stdx::obj_arg_t<decltype(&func_no_args)>>);
STATIC_REQUIRE(std::is_same_v<S, stdx::obj_arg_t<decltype(&S::f)>>);
STATIC_REQUIRE(
std::is_same_v<S_C const, stdx::obj_arg_t<decltype(&S_C::f)>>);
STATIC_REQUIRE(
std::is_same_v<S_V volatile, stdx::obj_arg_t<decltype(&S_V::f)>>);
STATIC_REQUIRE(std::is_same_v<S_CV const volatile,
stdx::obj_arg_t<decltype(&S_CV::f)>>);
}
TEST_CASE("object argument types (value categories)", "[function_traits]") {
STATIC_REQUIRE(std::is_same_v<S_LV &, stdx::obj_arg_t<decltype(&S_LV::f)>>);
STATIC_REQUIRE(
std::is_same_v<S_RV &&, stdx::obj_arg_t<decltype(&S_RV::f)>>);
STATIC_REQUIRE(
std::is_same_v<S_CLV const &, stdx::obj_arg_t<decltype(&S_CLV::f)>>);
STATIC_REQUIRE(
std::is_same_v<S_CRV const &&, stdx::obj_arg_t<decltype(&S_CRV::f)>>);
STATIC_REQUIRE(
std::is_same_v<S_VLV volatile &, stdx::obj_arg_t<decltype(&S_VLV::f)>>);
STATIC_REQUIRE(std::is_same_v<S_VRV volatile &&,
stdx::obj_arg_t<decltype(&S_VRV::f)>>);
STATIC_REQUIRE(std::is_same_v<S_CVLV const volatile &,
stdx::obj_arg_t<decltype(&S_CVLV::f)>>);
STATIC_REQUIRE(std::is_same_v<S_CVRV const volatile &&,
stdx::obj_arg_t<decltype(&S_CVRV::f)>>);
}
TEST_CASE("object argument type (lambda)", "[function_traits]") {
[[maybe_unused]] auto const x = [](int) {};
STATIC_REQUIRE(std::is_same_v<decltype(x), stdx::obj_arg_t<decltype(x)>>);
}
namespace {
struct TS {
template <typename T> auto operator()(T) -> void {}
};
struct TS_C {
template <typename T> auto operator()(T) const -> void {}
};
struct TS_V {
template <typename T> auto operator()(T) volatile -> void {}
};
struct TS_CV {
template <typename T> auto operator()(T) const volatile -> void {}
};
struct TS_LV {
template <typename T> auto operator()(T) & -> void {}
};
struct TS_RV {
template <typename T> auto operator()(T) && -> void {}
};
struct TS_CLV {
template <typename T> auto operator()(T) const & -> void {}
};
struct TS_CRV {
template <typename T> auto operator()(T) const && -> void {}
};
struct TS_VLV {
template <typename T> auto operator()(T) volatile & -> void {}
};
struct TS_VRV {
template <typename T> auto operator()(T) volatile && -> void {}
};
struct TS_CVLV {
template <typename T> auto operator()(T) const volatile & -> void {}
};
struct TS_CVRV {
template <typename T> auto operator()(T) const volatile && -> void {}
};
} // namespace
TEST_CASE("member function template arity", "[function_traits]") {
STATIC_REQUIRE(stdx::arity_v<TS> == 1);
STATIC_REQUIRE(stdx::arity_v<TS_C> == 1);
STATIC_REQUIRE(stdx::arity_v<TS_V> == 1);
STATIC_REQUIRE(stdx::arity_v<TS_CV> == 1);
STATIC_REQUIRE(stdx::arity_v<TS_LV> == 1);
STATIC_REQUIRE(stdx::arity_v<TS_RV> == 1);
STATIC_REQUIRE(stdx::arity_v<TS_CLV> == 1);
STATIC_REQUIRE(stdx::arity_v<TS_CRV> == 1);
STATIC_REQUIRE(stdx::arity_v<TS_VLV> == 1);
STATIC_REQUIRE(stdx::arity_v<TS_VRV> == 1);
STATIC_REQUIRE(stdx::arity_v<TS_CVLV> == 1);
STATIC_REQUIRE(stdx::arity_v<TS_CVRV> == 1);
}