-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy path257.md
207 lines (166 loc) · 5.04 KB
/
257.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
<details open><summary>Info</summary><p>
* **Did you know that static reflection can be used to implement row polymorphism?**
* https://wg21.link/P0385
</p></details><details open><summary>Example</summary><p>
```cpp
struct foo {
int a{};
int b{};
};
struct bar {
int a{};
};
struct missing_a {
int b{};
};
struct row_with_member_a {
constexpr explicit(false) row_with_member_a(const auto& t)
: a{t.a}
{ }
int a{};
};
auto shrink(row_with_member_a r) {
std::cout << r.a;
}
int main() {
//shrink(missing_a{.b = 42}); // error
shrink(foo{.a = 4, .b = 2}); // prints 4
shrink(bar{.a = 42}); // prints 42
}
```
> https://godbolt.org/z/arxnx1xaj
</p></details><details open><summary>Puzzle</summary><p>
* **Can you implement a generic shrink function which uses row polymorphism technique to shrink input type and returns string output?**
```cpp
template <fixed_string Name, class TValue = void*>
struct row {
static constexpr auto name = Name;
TValue value{};
template <class T>
constexpr auto operator=(const T& t) {
return row<Name, T>{.value = t};
}
};
template <fixed_string Name>
constexpr auto operator""_row() {
return row<Name>{};
}
template <class... Ts>
struct rows; // TODO
auto shrink(rows<row<"a", int>, row<"b", int>> t) {
return std::to_string(t["a"_row]) + ", " + std::to_string(t["b"_row]);
}
struct empty {};
struct nope {
int a{};
};
struct foo {
int a{};
int b{};
};
struct bar {
int c{};
int b{};
int a{};
};
int main() {
using namespace boost::ut;
static_assert(not [](auto t) { return requires { shrink(t); };}(empty{}));
static_assert(not [](auto t) { return requires { shrink(t); }; }(nope{}));
static_assert([](auto t) { return requires { shrink(t); }; }(foo{}));
static_assert([](auto t) { return requires { shrink(t); }; }(bar{}));
using std::literals::string_literals::operator""s;
expect("1, 2"s == shrink(foo{.a = 1, .b = 2}));
expect("2, 4"s == shrink(bar{.c = 8, .b = 4, .a = 2}));
}
```
> https://godbolt.org/z/crGqYjfoT
</p></details><details><summary>Solutions</summary><p>
```cpp
template <class T, fixed_string Name>
concept has_member_named = []<class... Ts>(type_list<Ts...>) {
return ((std::string_view{meta::get_name_v<Ts>} == Name) or ...);
}(meta::unpack_sequence_t<type_list, meta::get_data_members_t<reflexpr(T)>>{});
template <class T, fixed_string... Names>
concept has_members_named = (has_member_named<T, Names> and ...);
template <class... TRows>
struct rows : TRows... {
template <has_members_named<TRows::name...> T>
rows(const T& t) {
for_each(get_data_members(get_aliased(mirror(T))),
[&](auto mo) {
if constexpr (constexpr std::string_view s = std::data(get_name(mo));
((s == TRows::name) or ...)) {
constexpr fixed_string<std::size(s)> name = std::data(s);
(*this)[row<name>{}] = get_value(mo, t);
}
});
}
template <class N>
constexpr auto operator[](N) -> decltype(auto) {
return [] <class T> (row<N::name, T>& t) -> auto& {
return t.value;
}(*this);
}
};
```
> https://godbolt.org/z/rj7Wjdxnb
```cpp
template <class...>
struct rows;
template <class T, fixed_string Name>
concept shrinkable = [] {
using namespace std::experimental::reflect;
return []<class... Ts>(std::tuple<Ts...>) {
return (... or (std::string_view{get_name_v<Ts>} == Name));
}
(unpack_sequence_t<std::tuple, get_data_members_t<reflexpr(T)>>{});
}();
template <class... Ts>
struct rows : Ts... {
template <class T>
requires(... and shrinkable<T, Ts::name>) constexpr rows(const T &t) {
for_each(get_data_members(mirror(T)), [&](auto member) {
if constexpr (constexpr std::string_view name = get_name(member);
(... or (name == Ts::name))) {
using key = row<fixed_string<size(name)>{data(name)}>;
(*this)[key{}] = get_value(member, t);
}
});
}
template <fixed_string V>
auto &operator[](row<V>) {
return []<class T>(row<V, T> &self) -> T & { return self.value; }(*this);
}
};
```
> https://godbolt.org/z/xv681zY3r
```cpp
template <class TData, class... TRows>
concept HasRows = []<class... TMembers>(std::tuple<TMembers...>) {
return (... and [](std::string_view name) {
return (... or (name == meta::get_name_v<TMembers>));
}(TRows::name));
}
(meta::unpack_sequence_t<std::tuple, meta::get_data_members_t<reflexpr(TData)>>{});
template <class... TRows>
struct rows : TRows... {
template <HasRows<TRows...> TData>
rows(const TData& data) {
for_each(get_data_members(mirror(TData)), [&](auto member) {
constexpr std::string_view name = get_name(member);
(..., [&] {
if constexpr (name == TRows::name) {
static_cast<TRows&>(*this).value = data.*get_pointer(member);
}
}());
});
};
template <fixed_string Name>
constexpr auto& operator[](row<Name>) {
return []<class TValue>(row<Name, TValue> & self) -> auto& { return self.value; }
(*this);
};
};
```
> https://godbolt.org/z/1fbh4z3bf