-
Notifications
You must be signed in to change notification settings - Fork 139
/
伊凡Gogh.cpp
92 lines (86 loc) · 2.01 KB
/
伊凡Gogh.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
#include <iostream>
#include <type_traits>
template <class T>
concept aggregate = std::is_aggregate_v<std::remove_cvref_t<T>>;
struct placeholder {
template <class T>
operator T() const
{
return T{};
}
};
template <aggregate T, placeholder... _Members>
consteval size_t size()
{
if constexpr (requires { T{ _Members... }; })
return size<T, _Members..., placeholder{}>();
else
return sizeof...(_Members) - 1;
}
void for_each_member(aggregate auto const &object, [[maybe_unused]] auto &&print_function)
{
using T = std::remove_cvref_t<decltype(object)>;
if constexpr (size<T>() == 1)
{
const auto &[member1] = object;
print_function(member1);
}
else if constexpr (size<T>() == 2)
{
const auto &[member1, member2] = object;
print_function(member1);
print_function(member2);
}
else if constexpr (size<T>() == 3)
{
const auto &[member1, member2, member3] = object;
print_function(member1);
print_function(member2);
print_function(member3);
}
else if constexpr (size<T>() == 4)
{
const auto &[member1, member2, member3, member4] = object;
print_function(member1);
print_function(member2);
print_function(member3);
print_function(member4);
}
}
int main()
{
struct A {
} a;
struct B {
std::string s{ "abcdef" };
} b;
struct C {
unsigned i{ 123456 };
const char *s = "ABCDEF";
} c;
struct D {
long long i{ 123456123456 };
std::string s{ "!@#$%^" };
double d{ 1.234e-56 };
} d;
struct E {
double a{ 1.2 }, b{ 3.4 }, c{ 5.6 }, d{ 7.8 };
} e;
struct F {
int a{ 1 }, b{ 2 }, c{ 3 }, d{ 4 }, e{ 5 };
} f;
std::cout << size<A>() << std::endl;
std::cout << size<B>() << std::endl;
std::cout << size<C>() << std::endl;
std::cout << size<D>() << std::endl;
std::cout << size<E>() << std::endl;
std::cout << size<F>() << std::endl;
auto print = [](const auto &member) { std::cout << member << ' '; };
for_each_member(a, print);
for_each_member(b, print);
for_each_member(c, print);
for_each_member(d, print);
for_each_member(e, print);
for_each_member(f, print);
return 0;
}