forked from Mq-b/Loser-HomeWork
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathakchilov.cpp
72 lines (63 loc) · 1.66 KB
/
akchilov.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
#include<type_traits>
#include<iostream>
#include<string>
//任意类
struct init
{
template<class T> operator T();
};
//从一个用一个递减的类型列表尝试能不能std::is_constructible_v
template<bool, class T, class construct_list, size_t cnt> struct count_size
{
static constexpr size_t value = cnt;
};
template<class T, template<class ...> class construct_list, size_t cnt, class First, class ...rest>
struct count_size<false, T, construct_list<First, rest...>, cnt>
{
static constexpr size_t value =
count_size<
std::is_constructible_v<T, First, rest...>,
T,
construct_list<rest...>,
(std::is_constructible_v<T, First, rest...>? cnt : cnt - 1)>
::value;
};
template<class ...> struct tl {};
template<class T> constexpr size_t size()
{
return count_size<false, std::decay_t<T>, tl<init, init, init, init>, 4>::value;
}
template<class T, class F> constexpr void for_each_member(T&& i, F&& f)
{
if constexpr (size<T>() == 4u)
{
auto&& [a, b, c, d] = i;
f(a), f(b), f(c), f(d);
}
if constexpr (size<T>() == 3u)
{
auto&& [a, b, c] = i;
f(a), f(b), f(c);
}
if constexpr (size<T>() == 2u)
{
auto&& [a, b] = i;
f(a), f(b);
}
if constexpr (size<T>() == 1u)
{
auto&& [a] = i;
f(a);
}
}
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);
}