-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy path350.md
67 lines (48 loc) · 1.46 KB
/
350.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
<details open><summary>Info</summary><p>
* **Did you know about C++26 proposal - Aggregates are named tuples?**
* https://wg21.link/P2141
</p></details><details open><summary>Example</summary><p>
```cpp
#include <tuple>
struct foo {
int i{};
bool b{};
float f{};
};
constexpr auto f = foo{.i = 42, .b = true, .f = 4.2f};
static_assert(42 == std::get<0>(f) and std::get<1>(f) and 4.2f == std::get<2>(f));
```
> https://godbolt.org/z/r5vozndxb
</p></details><details open><summary>Puzzle</summary><p>
* **Can you extend std::get to support aggregates?**
```cpp
#include <tuple>
struct foo {
int i{};
bool b{};
float f{};
};
constexpr auto f = foo{.i = 42, .b = true, .f = 4.2};
static_assert(42 == std::get<0>(f) and std::get<1>(f) and 4.2 == std::get<2>(f));
```
> https://godbolt.org/z/MfWEcdh7s
</p></details>
</p></details><details><summary>Solutions</summary><p>
```cpp
template <typename T, std::size_t I, typename = void>
struct has_get : std::false_type {};
template <typename T, std::size_t I>
struct has_get<T, I, std::void_t<decltype(std::get<I>(std::declval<T>()))>>
: std::true_type {};
namespace std {
template <typename T>
concept DoesNotHaveGetConcept = requires(T t) { !has_get<T, 0>::value; };
namespace mp = boost::mp;
template <std::size_t I>
constexpr auto get(DoesNotHaveGetConcept auto s) {
return std::get<I>(mp::reflection::to_tuple(s));
}
} // namespace std
```
> https://godbolt.org/z/WsTj1Tr79
</p></details>