forked from Mq-b/Loser-HomeWork
-
Notifications
You must be signed in to change notification settings - Fork 0
/
涼風青葉.cpp
44 lines (39 loc) · 1.14 KB
/
涼風青葉.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
#include<iostream>
struct Any {
template<typename T>
operator T();
};
template<typename T, typename ...Args>
constexpr std::size_t size(Args ...args) {
if constexpr (requires {T{ args... };})
return size<T>(args..., Any{});
else
return sizeof...(args) - 1;
}
template<typename T, typename Func>
void for_each_member(const T &arg, const Func &func) {
if constexpr (size<T>() == 1) {
const auto &[m1] = arg;
func(m1);
} else if constexpr (size<T>() == 2) {
const auto &[m1, m2] = arg;
func(m1), func(m2);
} else if constexpr (size<T>() == 3) {
const auto &[m1, m2, m3] = arg;
func(m1), func(m2), func(m3);
} else if constexpr (size<T>() == 4) {
const auto &[m1, m2, m3, m4] = arg;
func(m1), func(m2), func(m3), func(m4);
}
}
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);
}