-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
365 - Did you know about C++26 static reflection proposal (5/N)?
- Loading branch information
1 parent
5ff2660
commit 1552a7a
Showing
2 changed files
with
73 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
<details open><summary>Info</summary><p> | ||
|
||
* **Did you know about C++26 static reflection proposal (5/N)?** | ||
|
||
* https://wg21.link/P2996 | ||
|
||
</p></details><details open><summary>Example</summary><p> | ||
|
||
```cpp | ||
struct foo; | ||
[[maybe_unused]] constexpr auto _ = | ||
define_class(^foo, {std::meta::nsdm_description(^int, {.name = "bar"})}); | ||
foo f{.bar = 42}; // has bar int member | ||
``` | ||
> https://godbolt.org/z/aqxanTe3r | ||
</p></details><details open><summary>Puzzle</summary><p> | ||
* **Can you implement template alias `pack` which will pack given struct by sorting members by their size?** | ||
```cpp | ||
template<class T> using pack; // TODO | ||
struct unpacked { | ||
short s; | ||
int i; | ||
bool b; | ||
}; | ||
static_assert(12 == sizeof(unpacked)); | ||
using packed = pack<unpacked>; | ||
static_assert(8 == sizeof(packed)); | ||
static_assert(requires(packed p) { p.i; p.s; p.b; }); | ||
``` | ||
|
||
> https://godbolt.org/z/hnPvsE9h3 | ||
</p></details> | ||
|
||
</p></details><details><summary>Solutions</summary><p> | ||
|
||
```cpp | ||
namespace detail { | ||
template<class T> struct packed; | ||
template<class T> [[nodiscard]] consteval auto pack() { | ||
std::vector members = std::meta::nonstatic_data_members_of(^T); | ||
sort(members, [](auto lhs, auto rhs) consteval { return std::meta::size_of(lhs) < std::meta::size_of(rhs); }); | ||
std::vector<std::meta::nsdm_description> new_members{}; | ||
for (const auto& member: members) { | ||
new_members.push_back({std::meta::type_of(member), {.name = std::meta::name_of(member)}}); | ||
} | ||
return define_class(^packed<T>, new_members); | ||
} | ||
} // namespace detail | ||
template<class T> using pack = [:detail::pack<T>():]; | ||
|
||
struct unpacked { | ||
short s; | ||
int i; | ||
bool b; | ||
}; | ||
static_assert(12 == sizeof(unpacked)); | ||
|
||
using packed = pack<unpacked>; | ||
static_assert(8 == sizeof(packed)); | ||
static_assert(requires(packed p) { p.i; p.s; p.b; }); | ||
``` | ||
> https://godbolt.org/z/ExYfTv4nK | ||
</p></details> |