-
Notifications
You must be signed in to change notification settings - Fork 75
/
Copy pathfor_typename.cxx
executable file
·66 lines (57 loc) · 1.74 KB
/
for_typename.cxx
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
#include <cstdio>
template<typename... types_t>
void func0() {
// Loop over parameter packs using sizeof... and ...[].
printf("func0:\n");
@meta for(int i = 0; i < sizeof...(types_t); ++i)
printf(" %s\n", types_t...[i].string);
}
void func1() {
printf("func1:\n");
@meta for typename(type_t : { char, double, long[3], char(short) })
printf(" %s\n", type_t.string);
}
template<typename... types_t>
void func2() {
// Loop over parameter pack using for-typename.
printf("func2:\n");
@meta for typename(type_t : { types_t... })
printf(" %s\n", type_t.string);
}
template<typename... types_t>
void func3() {
// Loop over an assortment of types inside braces.
// Hit an int at the start, then the even elements of types_t, then
// a char[1].
printf("func3:\n");
@meta for typename(type_t : { int, types_t...[::2] ..., char[1]})
printf(" %s\n", type_t.string);
}
enum typename my_types_t {
int, void, char, wchar_t[10]
};
template<typename list_t>
void func4() {
static_assert(__is_typed_enum(list_t));
// Expand a typed enum into your for typename braces.
printf("func4:\n");
@meta for typename(type_t : { long, @enum_types(list_t)..., char16_t })
printf(" %s\n", type_t.string);
}
template<typename list_t>
void func5() {
static_assert(__is_typed_enum(list_t));
// Alternatively, ditch the braces and use the 'enum' keyword to
// specify we want iteration over all types in the typed enum.
printf("func5:\n");
@meta for typename(type_t : enum list_t)
printf(" %s\n", type_t.string);
}
int main() {
func0<double, int*, char[3], short(long)>();
func1();
func2<float, short*, void*()>();
func3<char, unsigned char, signed char, char16_t, char32_t>();
func4<my_types_t>();
func5<my_types_t>();
}