forked from Mq-b/Loser-HomeWork
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmifeng.cpp
90 lines (76 loc) · 1.71 KB
/
mifeng.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
// ConsoleApplication1.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
#include <iostream>
template <size_t s>
struct tag : tag<s - 1> {};
template <>
struct tag<0> {};
struct init
{
template <class T>
operator T();
};
template <class T>
constexpr auto size(tag<4>) -> decltype(T{ init{}, init{}, init{}, init{} }, 0u)
{
return 4u;
}
template <class T>
constexpr auto size(tag<3>) -> decltype(T{ init{}, init{}, init{} }, 0u)
{
return 3u;
}
template <class T>
constexpr auto size(tag<2>) -> decltype(T{ init{}, init{} }, 0u)
{
return 2u;
}
template <class T>
constexpr auto size(tag<1>) -> decltype(T{ init{} }, 0u)
{
return 1u;
}
template <class T>
requires std::is_aggregate_v<T>
constexpr size_t size()
{
return size<T>(tag<4>{});
}
template <class T, class F>
requires std::is_aggregate_v<T>
constexpr void for_each_member(const T& obj, F func)
{
if constexpr (size<T>() == 4)
{
const auto& [m0, m1, m2, m3] = obj;
func(m0); func(m1); func(m2); func(m3);
}
else if constexpr (size<T>() == 3)
{
const auto& [m0, m1, m2] = obj;
func(m0); func(m1); func(m2);
}
else if constexpr (size<T>() == 2)
{
const auto& [m0, m1] = obj;
func(m0); func(m1);
}
else if constexpr (size<T>() == 1)
{
const auto& [m0] = obj;
func(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);
}