-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy path262.md
203 lines (155 loc) · 5 KB
/
262.md
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
<details open><summary>Info</summary><p>
* **Did you know that type_info equality operator is constexpr in C++23?**
* https://wg21.link/p1328
</p></details><details open><summary>Example</summary><p>
```cpp
#include <typeinfo>
#include <type_traits>
static_assert(std::is_same_v<int, int>);
static_assert(typeid(int) == typeid(int));
static_assert(typeid(int) == typeid(const int));
static_assert(not std::is_same_v<int, const int>);
static_assert(typeid(int) == typeid(const int&));
static_assert(not std::is_same_v<int, const int&>);
```
> https://godbolt.org/z/jddq7s1o4
</p></details><details open><summary>Puzzle</summary><p>
* **Can you implement `compare_types` function which compare given types by leveraging typeid comparison?**
```cpp
#include <typeinfo>
template<class... Ts>
[[nodiscard]] consteval auto compare_types() -> bool; // TODO
static_assert(compare_types());
static_assert(compare_types<int>());
static_assert(compare_types<void>());
static_assert(compare_types<int, int>());
static_assert(compare_types<const int, int, int const>());
static_assert(compare_types<const int&, int>());
static_assert(compare_types<int&, int&&>());
static_assert(compare_types<int, int&&, int&, const int&, int const&&>());
static_assert(compare_types<int&, const int&>());
static_assert(compare_types<void, void, void>());
static_assert(not compare_types<int, float>());
static_assert(not compare_types<void*, const void*, void* const>());
static_assert(not compare_types<int, int*>());
static_assert(not compare_types<int*, int**>());
```
> https://godbolt.org/z/8P73zn9qP
</p></details><details><summary>Solutions</summary><p>
```cpp
template <class T, class U>
using types_equal_t = std::bool_constant<typeid(T) == typeid(U)>;
template<class... Ts>
[[nodiscard]] consteval auto compare_types() -> bool {
using pairwise_equality_t = boost::mp11::mp_pairwise_fold<boost::mp11::mp_list<Ts...>, types_equal_t>;
return boost::mp11::mp_apply<boost::mp11::mp_all, pairwise_equality_t>::value;
}
```
> https://godbolt.org/z/nqjrbGrzf
```cpp
consteval auto compare_types() -> bool{ return true; }
template<class T>
[[nodiscard]] consteval auto compare_types() -> bool{ return true; }
template<class R, class T, class... Ts>
[[nodiscard]] consteval auto compare_types() -> bool{
return typeid(R) == typeid(T) && compare_types<T,Ts...>();
}
```
> https://godbolt.org/z/o3jPvPor5
```cpp
template<class... Ts>
[[nodiscard]] consteval auto compare_types() -> bool
{
if constexpr (sizeof...(Ts) == 0) {
return true;
}
else {
auto helper = []<typename H, typename... Hs>() {
return ((typeid(H) == typeid(Hs)) && ...);
};
return helper.template operator()<Ts...>();
}
}
```
> https://godbolt.org/z/cGne6Y5xa
```cpp
[nodiscard]] consteval auto compare_types() -> bool {
return true;
}
template <class T, class... Ts>
[[nodiscard]] consteval auto compare_types() -> bool {
if constexpr (sizeof...(Ts) == 0) {
return true;
} else {
return (... and (typeid(T) == typeid(Ts)));
}
```
> https://godbolt.org/z/469d6oPc5
```cpp
template<class... Ts>
[[nodiscard]] consteval bool compare_types();
template<class A, class B, class... Ts>
[[nodiscard]] consteval auto compare_2types_and_a_seq_of_types() -> bool {
return (typeid(A) == typeid(B)) && (compare_types<A, Ts...>());
}
template<class... Ts>
[[nodiscard]] consteval auto compare_types() -> bool{
if constexpr(sizeof...(Ts) > 1){ return compare_2types_and_a_seq_of_types<Ts...>(); }
else { return true; }
}
```
> https://godbolt.org/z/5ePox7vEh
```cpp
template <class... Ts>
[[nodiscard]] constexpr auto compare_types() -> bool {
const std::type_info* type_ptrs[]{&typeid(std::remove_cvref_t<Ts>)...};
for (auto type_ptr : type_ptrs) {
if (*type_ptr != **type_ptrs) return false;
}
return true;
}
```
> https://godbolt.org/z/4c6qK3aoE
```cpp
[[nodiscard]] consteval bool compare_types() {
return true;
}
template<class T1>
[[nodiscard]] consteval bool compare_types() {
return true;
}
template<class T1, class T2, class...Ts>
[[nodiscard]] consteval bool compare_types() {
return typeid(T1) == typeid(T2) and compare_types<T2, Ts...>();
}
```
> https://godbolt.org/z/ee8a7sKxh
```cpp
template<typename ... Ts>
struct types{};
template<class... Ts>
consteval bool compare_types(){
constexpr auto n = sizeof...(Ts);
if constexpr (n == 0 || n == 1){ return true;
} else {
return []<typename U1, typename U2, typename ... Us>(types<U1, U2, Us ...>){
return typeid(U1) == typeid(U2) && compare_types<U2, Us ...>();
}(types<Ts...>{});
}
}
```
> https://godbolt.org/z/K9n1erMxa
```cpp
template<class... Ts>
[[nodiscard]] consteval auto compare_types() -> bool {
if constexpr( sizeof...(Ts) < 1)
return true;
else {
auto all_equal = []<typename T1, typename...Tr>() {
return ((typeid(T1) == typeid(Tr)) && ...);
};
return all_equal.template operator()<Ts...>();
}
}
```
> https://godbolt.org/z/TMTEsEzxc