-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy path362.md
70 lines (49 loc) · 1.5 KB
/
362.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
<details open><summary>Info</summary><p>
* **Did you know about C++26 static reflection proposal (2/N)?**
* https://wg21.link/P2996
</p></details><details open><summary>Example</summary><p>
```cpp
struct foo {
int a{};
int b{};
int c{};
};
static_assert(3 == std::size(std::meta::nonstatic_data_members_of(^foo)));
```
> https://godbolt.org/z/hr8WvMGYG
</p></details><details open><summary>Puzzle</summary><p>
* **Can you implement `get` and `get_name` for struct members?**
```cpp
template<auto N, class T>
[[nodiscard]] constexpr auto get(const T& t) -> decltype(auto); // TODO
template<auto N, class T>
[[nodiscard]] constexpr auto get_name(const T& t) -> std::string_view; // TODO
struct foo {
int a{};
int b{};
int c{};
};
constexpr foo f{.a=1, .b=2, .c=3};
static_assert(1 == get<0>(f));
static_assert(2 == get<1>(f));
static_assert(3 == get<2>(f));
using std::literals::operator""sv;
static_assert("a"sv == get_name<0>(f));
static_assert("b"sv == get_name<1>(f));
static_assert("c"sv == get_name<2>(f));
```
> https://godbolt.org/z/YGjnhWhzK
</p></details>
</p></details><details><summary>Solutions</summary><p>
```cpp
template<auto N, class T>
[[nodiscard]] constexpr auto get(const T& t) -> decltype(auto) {
return t.[:std::meta::nonstatic_data_members_of(^T)[N]:];
}
template<auto N, class T>
[[nodiscard]] constexpr auto get_name(const T& t) -> std::string_view {
return std::meta::name_of(std::meta::nonstatic_data_members_of(^T)[N]);
}
```
> https://godbolt.org/z/qK7K948jn
</p></details>