-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy path250.md
292 lines (229 loc) · 7.05 KB
/
250.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
<details open><summary>Info</summary><p>
* **Did you know about methods to access the last element of variadic pack...?**
</p></details><details open><summary>Example</summary><p>
```cpp
template<class, std::size_t> concept Any = true;
constexpr auto last1 = [](auto... args) {
return [&]<std::size_t... Ns>(std::index_sequence<Ns...>) {
return [](Any<Ns> auto..., auto last) {
return last;
}(args...);
}
(std::make_index_sequence<sizeof...(args) - 1>{});
};
auto last2 = [](auto... args) {
return (args, ...);
};
static_assert(1 == last1(1));
static_assert(2 == last1(1, 2));
static_assert(3 == last1(1, 2, 3));
static_assert(1 == last2(1));
static_assert(2 == last2(1, 2));
static_assert(3 == last2(1, 2, 3));
```
> https://godbolt.org/z/599K9eah8
</p></details><details open><summary>Puzzle</summary><p>
* **Can you implement nth_element which returns Nth element of variadic pack...?**
```cpp
template<auto N>
constexpr auto nth_element; // TODO
static_assert(1 == nth_element<0>(1));
static_assert(1 == nth_element<0>(1, 2));
static_assert(2 == nth_element<1>(1, 2));
static_assert(1 == nth_element<0>(1, 2, 3));
static_assert(2 == nth_element<1>(1, 2, 3));
static_assert(3 == nth_element<2>(1, 2, 3));
```
> https://godbolt.org/z/1aPcePYPE
</p></details><details><summary>Solutions</summary><p>
```cpp
template<auto N>
constexpr auto nth_element = [](auto...args){
return std::get<N>(std::tuple{args...});
};
```
> https://godbolt.org/z/vqKfzevbK
```cpp
template <typename, std::size_t> concept prefix = true;
template <auto N>
constexpr auto nth_element(auto... args) {
return [&]<auto... Is>(std::index_sequence<Is...>) {
return [] (prefix<Is> auto..., auto arg, auto...) {
return arg;
}(args...);
}(std::make_index_sequence<N>());
}
```
> https://godbolt.org/z/WPhe5MerW
```cpp
template<class T, std::size_t N> struct any { T value{}; };
template<std::size_t N, class T>
constexpr decltype(auto) get(any<T, N>& t) { return t.value; }
template<auto N>
constexpr auto nth_element = [](auto... args) {
return [&]<std::size_t... Ns>(std::index_sequence<Ns...>) {
struct : any<decltype(args), Ns>... { } _{args...};
return get<N>(_);
}
(std::make_index_sequence<sizeof...(args)>{});
};
```
> https://godbolt.org/z/aEM31WE8b
```cpp
template<auto N>
constexpr auto nth_element = [](auto... args) {
return [&]<std::size_t... Ns>(std::index_sequence<Ns...>) {
return [](decltype((void*)Ns)..., auto* nth, auto*...) {
return *nth;
}(&args...);
}
(std::make_index_sequence<N>{});
};
```
> https://godbolt.org/z/a8vsqM848
```cpp
template<class T, std::size_t N> struct any { T value{}; };
template<auto N>
constexpr auto nth_element = [](auto... args) {
return [&]<std::size_t... Ns>(std::index_sequence<Ns...>) {
struct : any<decltype(args), Ns>... { } _{args...};
return static_cast<__type_pack_element<N, any<decltype(args), Ns>...>&>(_).value;
}
(std::make_index_sequence<sizeof...(args)>{});
};
```
> https://godbolt.org/z/GMGnWfvnE
```cpp
template<auto N>
constexpr auto nth_element = [](auto arg, auto... args)
requires (std::is_same_v<decltype(arg), decltype(args)> and ...) {
return std::array{arg, args...}[N];
};
```
> https://godbolt.org/z/1G7Gj16nx
```cpp
template<auto N, auto I = 0>
constexpr auto nth_element(auto arg, auto... args) {
if constexpr (I == N) {
return arg;
} else if constexpr (sizeof...(args) > 0u) {
return nth_element<N, I + 1>(args...);
}
}
```
> https://godbolt.org/z/vGh1nTnaq
```cpp
template<class T, std::size_t N> struct any { T value{}; };
template<auto N>
constexpr auto nth_element = [](auto... args) {
return [&]<std::size_t... Ns>(std::index_sequence<Ns...>) {
struct : any<decltype(args), Ns>... { } _{args...};
return static_cast<boost::mp11::mp_at_c<boost::mp11::mp_inherit<any<decltype(args), Ns>...>, N>&>(_).value;
}
(std::make_index_sequence<sizeof...(args)>{});
};
```
> https://godbolt.org/z/rKjzPf9zK
```cpp
template<class, std::size_t> concept Any = true;
template<auto N>
constexpr auto nth_element(auto... pack) {
return [&]<auto... Is>(std::index_sequence<Is...>) {
return [](Any<Is> auto..., auto arg, auto...) {
return arg;
}(pack...);
}(std::make_index_sequence<N>{});
}
```
> https://godbolt.org/z/bao1dsY5q
```cpp
template<class, std::size_t> concept Any = true;
template<auto N>
constexpr auto nth_element( auto ... args )
{
return [&]<std::size_t... Ns>(std::index_sequence<Ns...>) {
return [](Any<Ns> auto ..., auto nth, auto ... ) {
return nth;
}(args...);
}(std::make_index_sequence<N>{});
}
```
> https://godbolt.org/z/78b1qd3cE
```cpp
namespace detail {
#define NTH_ARG(Z, N, TYPE) TYPE arg##N,
#define NTH_ELEMENT(Z, N, TYPE) \
template <auto I> \
requires(I == N) constexpr decltype(auto) nth_element( \
BOOST_PP_REPEAT(BOOST_PP_INC(N), NTH_ARG, TYPE) TYPE...) { \
return arg##N; \
}
BOOST_PP_REPEAT(BOOST_PP_DEC(BOOST_PP_LIMIT_REPEAT), NTH_ELEMENT, auto &&)
#undef NTH_ELEMENT
#undef NTH_ARG
} // namespace detail
template <auto N> constexpr decltype(auto) nth_element(auto &&...args) {
return detail::nth_element<N>(args...);
}
```
> https://godbolt.org/z/ervdav89b
```cpp
template <class, std::size_t>
concept Any = true;
template <auto N> constexpr decltype(auto) nth_element(auto &&...args) {
return [&]<auto... Is>(std::index_sequence<Is...>) -> decltype(auto) {
return [](Any<Is> auto &&..., auto &&arg, auto &&...) -> decltype(auto) {
return arg;
}(args...);
}(std::make_index_sequence<N>{});
}
```
> https://godbolt.org/z/515fP7WbW
```cpp
template <std::size_t N>
requires (N == 0)
constexpr auto nth_element(auto&& arg0, auto&&...) {
return arg0;
}
template <std::size_t N>
requires (N > 0) constexpr auto nth_element(auto&& arg0, auto&&... rest) {
return nth_element<N-1>(std::forward<decltype(rest)>(rest)...);
}
```
> https://godbolt.org/z/5WqMc5bjo
```cpp
template<auto N>
constexpr auto nth_element = [](auto... args) {
static_assert(N < sizeof...(args));
return std::array{args...}[N];
};
```
> https://godbolt.org/z/vGnM4GnE4
```cpp
template<auto N>
constexpr decltype(auto) nth_element(auto &&... args) {
using namespace boost::mp11;
return [&] <class... Ts> (mp_list<Ts...>) -> decltype(auto) {
return [] (Ts &&..., auto &&arg, auto &&...) -> decltype(auto) {
return arg;
}(std::forward<decltype(args)>(args)...);
}(mp_take_c<mp_list<decltype(args)...>, N>{});
}
```
> https://godbolt.org/z/zEM1eGqxK
```cpp
template<std::size_t> struct Any
{
template<typename T > constexpr Any(T){}
};
template<auto N>
constexpr auto nth_element( auto ... args )
{
return [&]<std::size_t... Ns>(std::index_sequence<Ns...>) {
return [](Any<Ns> ..., auto nth, auto ... ) {
return nth;
}(args...);
}(std::make_index_sequence<N>{});
}
```
> https://godbolt.org/z/3471fcrqG