forked from Mq-b/Loser-HomeWork
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jacky.cpp
94 lines (81 loc) · 1.8 KB
/
jacky.cpp
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
//!gcc !clang
import std;
struct init {
template <typename T>
operator T();
};
template <size_t I>
struct tag : tag<I - 1> { };
template <>
struct tag<0> { };
template <typename T>
requires requires { T { init {}, init {}, init {}, init {} }; }
constexpr auto size_(tag<4>)
{
return 4u;
}
template <typename T>
requires requires { T { init {}, init {}, init {} }; }
constexpr auto size_(tag<3>)
{
return 3u;
}
template <typename T>
requires requires { T { init {}, init {} }; }
constexpr auto size_(tag<2>)
{
return 2u;
}
template <typename T>
requires requires { T { init {} }; }
constexpr auto size_(tag<1>)
{
return 1u;
}
template <typename T>
requires requires { T {}; }
constexpr auto size_(tag<0>)
{
return 0u;
}
template <typename T>
constexpr size_t size()
{
static_assert(std::is_aggregate_v<T>);
return size_<T>(tag<4> {}); // highest supported number
}
template <typename T, typename F>
void for_each_member(T const& v, F f)
{
static_assert(std::is_aggregate_v<T>);
if constexpr (size<T>() == 4u) {
const auto& [m0, m1, m2, m3] = v;
f(m0);
f(m1);
f(m2);
f(m3);
} else if constexpr (size<T>() == 3u) {
const auto& [m0, m1, m2] = v;
f(m0);
f(m1);
f(m2);
} else if constexpr (size<T>() == 2u) {
const auto& [m0, m1] = v;
f(m0);
f(m1);
} else if constexpr (size<T>() == 1u) {
const auto& [m0] = v;
f(m0);
}
}
int main() {
struct X { std::string s{ " " }; }x;
struct Y { double a{}, b{}, c{}, d{}; }y;
std::cout << size<X>() << '\n';
std::cout << size<Y>() << '\n';
auto print = [](const auto& member) {
std::cout << member << ' ';
};
for_each_member(x, print);
for_each_member(y, print);
}