-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy path359.md
226 lines (189 loc) · 6.56 KB
/
359.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
<details open><summary>Info</summary><p>
* **Did you know that C++20 `source_location` can be used to get the member names?**
* https://wg21.link/P1208
</p></details><details open><summary>Example</summary><p>
```cpp
namespace detail {
template <class, auto>
[[nodiscard]] consteval auto member_name() -> std::string_view {
return std::source_location::current().function_name();
}
template <class T> extern const T external;
consteval auto get(auto& obj) {
auto& [p1] = obj;
return &p1;
}
} // namespace deatil
template <class T>
constexpr auto member_name = detail::member_name<T, detail::get(detail::external<T>)>();
struct foo {
int bar;
};
static_assert(member_name<foo>.find("bar") != std::string_view::npos);
```
> https://godbolt.org/z/sMKMWa35W
</p></details><details open><summary>Puzzle</summary><p>
* **Can you implement `to_tuple` which returns a tuple from a struct with names?**
```cpp
template <fixed_string Name, class T>
struct named {
static constexpr auto name = Name;
T value{};
};
[[nodiscard]] consteval auto to_tuple(auto&& t); // TODO
struct foo {
int a;
int b;
};
constexpr auto t = to_tuple(foo{.a=42, .b=87});
static_assert("a" == std::get<0>(t).name and 42 == std::get<0>(t).value);
static_assert("b" == std::get<1>(t).name and 87 == std::get<1>(t).value);
```
> https://godbolt.org/z/fWxPdj7v6
</p></details>
</p></details><details><summary>Solutions</summary><p>
```cpp
template <std::size_t N>
class fixed_string final {
public:
constexpr explicit(true) fixed_string(const auto... cs) : data{cs...} {}
constexpr explicit(false) fixed_string(const char (&str)[N + 1]) {
std::copy_n(str, N + 1, std::data(data));
}
[[nodiscard]] constexpr auto operator<=>(const fixed_string&) const =
default;
[[nodiscard]] constexpr explicit(false) operator std::string_view() const {
return {std::data(data), N};
}
[[nodiscard]] constexpr auto size() const -> std::size_t { return N; }
std::array<char, N + 1> data{};
};
template <std::size_t N>
fixed_string(const char (&str)[N]) -> fixed_string<N - 1>;
template <fixed_string Name, class T>
struct named {
static constexpr auto name = Name;
T value{};
};
struct any_type {
template <class T>
constexpr operator T();
};
template <class TPtr>
struct ptr {
const TPtr* ptr;
};
namespace detail {
template <class T>
extern const T external;
struct any_type {
template <class T>
constexpr operator T();
};
template <class TPtr>
struct ptr {
const TPtr* ptr;
};
template <auto N, class T>
[[nodiscard]] constexpr auto nth_ptr(T&& t) {
if constexpr (requires { T{any_type{}, any_type{}, any_type{}}; }) {
auto&& [p1, p2, p3] = t;
if constexpr (N == 0) return ptr<decltype(p1)>{&p1};
if constexpr (N == 1) return ptr<decltype(p2)>{&p2};
if constexpr (N == 2) return ptr<decltype(p3)>{&p3};
} else if constexpr (requires { T{any_type{}, any_type{}}; }) {
auto&& [p1, p2] = t;
if constexpr (N == 0) return ptr<decltype(p1)>{&p1};
if constexpr (N == 1) return ptr<decltype(p2)>{&p2};
} else if constexpr (requires { T{any_type{}}; }) {
auto&& [p1] = t;
if constexpr (N == 0) return ptr<decltype(p1)>{&p1};
}
}
template <auto Ptr>
[[nodiscard]] consteval auto get_name() -> std::string_view {
return std::source_location::current().function_name();
}
template <auto N, class T>
constexpr auto get_name_impl =
detail::get_name<detail::nth_ptr<N>(detail::external<T>)>();
struct $struct$ {
int $field$;
};
constexpr auto $name = get_name_impl<0, detail::$struct$>;
constexpr auto $end =
$name.substr($name.find("$field$") + sizeof("$field$") - 1);
constexpr auto $begin = $name[$name.find("$field$") - 1];
} // namespace detail
template <auto N, class T>
constexpr auto get_name = [] {
const auto name = detail::get_name_impl<N, T>;
const auto begin = name.find(detail::$end);
const auto tmp = name.substr(0, begin);
return tmp.substr(tmp.find_last_of(detail::$begin) + 1);
}();
template <auto N>
[[nodiscard]] consteval auto nth(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>{});
}
template <auto N, class T>
constexpr auto get(T&& t) {
if constexpr (requires { T{any_type{}, any_type{}, any_type{}}; }) {
auto&& [p1, p2, p3] = t;
// structure bindings is not constexpr :/
if constexpr (N == 0) return ptr<decltype(p1)>{&p1};
if constexpr (N == 1) return ptr<decltype(p2)>{&p2};
if constexpr (N == 2) return ptr<decltype(p3)>{&p3};
} else if constexpr (requires { T{any_type{}, any_type{}}; }) {
auto&& [p1, p2] = t;
// structure bindings is not constexpr :/
if constexpr (N == 0) return ptr<decltype(p1)>{&p1};
if constexpr (N == 1) return ptr<decltype(p2)>{&p2};
} else if constexpr (requires { T{any_type{}}; }) {
auto&& [p1] = t;
// structure bindings is not constexpr :/
if constexpr (N == 0) return ptr<decltype(p1)>{&p1};
}
}
template <class T, auto N>
[[nodiscard]] consteval auto member_name() {
constexpr auto name = get_name<N, T>;
return [&]<auto... Ns>(std::index_sequence<Ns...>) {
return fixed_string<sizeof...(Ns)>{name[Ns]...};
}(std::make_index_sequence<name.size()>{});
}
template <class T>
[[nodiscard]] constexpr auto to_tuple(const T& t) {
if constexpr (requires { T{any_type{}, any_type{}, any_type{}}; }) {
auto&& [p1, p2, p3] = t;
return std::tuple(
named<member_name<T, 0>(), decltype(p1)>{.value = p1},
named<member_name<T, 1>(), decltype(p2)>{.value = p2},
named<member_name<T, 2>(), decltype(p2)>{.value = p3});
} else if constexpr (requires { T{any_type{}, any_type{}}; }) {
auto&& [p1, p2] = t;
return std::tuple(
named<member_name<T, 0>(), decltype(p1)>{.value = p1},
named<member_name<T, 1>(), decltype(p2)>{.value = p2});
} else if constexpr (requires { T{any_type{}}; }) {
auto&& [p1] = t;
return std::tuple(
named<member_name<T, 0>(), decltype(p1)>{.value = p1});
} else {
return std::tuple();
}
}
struct foo {
int a;
int b;
};
constexpr auto t = to_tuple(foo{.a=42, .b=87});
static_assert("a" == std::get<0>(t).name and 42 == std::get<0>(t).value);
static_assert("b" == std::get<1>(t).name and 87 == std::get<1>(t).value);
```
> https://godbolt.org/z/beq5reqdE
</p></details>