-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy path231.md
179 lines (140 loc) · 4.11 KB
/
231.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
<details open><summary>Info</summary><p>
* **Did you know about C++17 variadic using declaration?**
* http://eel.is/c++draft/namespace.udecl#nt:using-declarator-list
</p></details><details open><summary>Example</summary><p>
```cpp
template<class... TEvents>
struct ihandler : ihandler<TEvents>... {
virtual ~ihandler() noexcept = default;
using ihandler<TEvents>::on...;
};
template<class TEvent> struct ihandler<TEvent> {
virtual ~ihandler() noexcept = default;
virtual auto on(const TEvent&) -> bool = 0;
};
int main() {
struct foo {};
struct bar {};
struct handler : ihandler<foo, bar> {
auto on(const foo&) -> bool override { return true; }
auto on(const bar&) -> bool override { return false; }
};
std::unique_ptr<ihandler<foo, bar>> h = std::make_unique<handler>();
assert(h->on(foo{}));
assert(not h->on(bar{}));
}
```
> https://godbolt.org/z/ffr1TaGhq
</p></details><details open><summary>Puzzle</summary><p>
* **Can you implement `handler` which will implement an interface `T` for given `Ts...` and returns `Ts::value`?**
```cpp
template<class... TEvents>
struct ihandler : ihandler<TEvents>... {
virtual ~ihandler() noexcept = default;
using ihandler<TEvents>::on...;
};
template<class TEvent> struct ihandler<TEvent> {
virtual ~ihandler() noexcept = default;
virtual auto on(const TEvent&) -> bool = 0;
};
template<template<class...> class T, class... Ts>
struct handler final; // TODO
int main() {
using namespace boost::ut;
"handler"_test = [] {
struct foo { bool value{}; };
struct bar { bool value{}; };
auto h = std::make_unique<handler<ihandler, foo, bar>>();
expect(h->on(foo{.value = true}) and
static_cast<ihandler<foo>*>(h.get())->on(foo{.value = true})
);
expect(not h->on(bar{.value = false}) and
not static_cast<ihandler<bar>*>(h.get())->on(bar{.value = false})
);
};
}
```
> https://godbolt.org/z/hez4KvcnM
</p></details><details><summary>Solutions</summary><p>
```cpp
template<class T, template<class...> class U>
struct handler_helper:U<T>
{
auto on(const T& t) -> bool override{
return t.value;
}
};
template<template<class...> class T, class... Ts>
struct handler final : handler_helper<Ts,T> ...
{
using handler_helper<Ts,T>::on...;
};
```
> https://godbolt.org/z/bhqMdMj5E
```cpp
namespace detail {
template<template<class...> class I, class T>
struct handler : I<T> {
auto on(const T& t) -> bool override {
return t.value;
}
};
} // namespace detail
template<template<class...> class T, class... Ts>
struct handler final : detail::handler<T, Ts>... {
using detail::handler<T, Ts>::on...;
};
```
> https://godbolt.org/z/vfvee6bP1
```cpp
namespace detail {
template <template<class> class Interface, class TEvent>
struct handler_impl : Interface<TEvent> {
auto on(const TEvent& event) -> bool override { return event.value; }
};
}
template <template<class> class Interface, class... TEvents>
struct handler final : detail::handler_impl<Interface, TEvents>... {
using detail::handler_impl<Interface, TEvents>::on...;
};
```
> https://godbolt.org/z/7xEsM16jM
```cpp
template<template<class...> class T, class... Ts>
struct handler final : handler<T, Ts>... {
using handler<T, Ts>::on...;
};
template<template<class...> class T, class TEvent>
struct handler<T, TEvent> : T<TEvent> {
auto on(const TEvent& event) -> bool final {
return event.value;
}
};
```
> https://godbolt.org/z/Pe491e6E1
```cpp
namespace detail {
template <template <class...> class TContainer, class T>
struct handler_impl : TContainer<T> {
[[nodiscard]] constexpr auto on(const T& t) -> bool override {
return t.value;
}
};
} // namespace detail
template <template <class...> class T, class... Ts>
struct handler final : detail::handler_impl<T, Ts>... {
using detail::handler_impl<T, Ts>::on...;
};
```
> https://godbolt.org/z/W1xavY13r
```cpp
template<class T>
struct on_method : ihandler<T> {
bool on(T const& t) { return t.value; }
};
template<template<class...> class T, class... Ts>
struct handler final : on_method<Ts>... {
using on_method<Ts>::on...;
};
```
> https://godbolt.org/z/8vG89YY11