TGViewer
C++ - Reddit C++ - Reddit @r_cpp · 230 subscribers
Post #25152 11
std::array<std::byte, info.total_size> storage;
};

Now to finally use it:

using Struct = MetaAggregate<
Field{ type<int>, "integer_1"_name },
Field{ type<int>, "integer_2"_name },
Field{ type<std::string>, "string_1"_name },
Field{ type<std::string>, "string_2"_name }
>;

int main()
{
Struct::dump_layout();

Struct blah{ 1, 2, "3", "4" };

std::println("By name:");
std::println("blah[\"integer_1\"_field] = {}", blah["integer_1"_field]);
std::println("blah[\"integer_2\"_field] = {}", blah["integer_2"_field]);
std::println("blah[\"string_1\"_field] = {}", blah["string_1"_field]);
std::println("blah[\"string_2\"_field] = {}", blah["string_2"_field]);

blah["string_1"_field] = "foo";

std::println("By index:");
std::println("blah.get<0>() = {}", blah.get<0>());
std::println("blah.get<1>() = {}", blah.get<1>());
std::println("blah.get<2>() = {}", blah.get<2>());
std::println("blah.get<3>() = {}", blah.get<3>());

std::println("Structured bindings:");
auto [a, b, c, d] = blah.refs();
std::println("a = {}", a);
std::println("b = {}", b);
std::println("c = {}", c);
std::println("d = {}", d);
}

It's ugly, but it works.

----

### Caveats

- Since we rely on placement `new`, it cannot be made `constexpr`, sadly;
- Usage is awkward compared to real `struct`s;
- Getting all the reference categories right is tricky, I probably missed something.

I would definitely **NOT** recommend using this in production, but it was kinda fun to see whether it was possible.

https://redd.it/1t6tzvc
@r_cpp
Reddit From the cpp community on Reddit: Poor man's define_aggregate Explore this post and more from the cpp community
More from @r_cpp
  1. Oct 3, 2026HPX 2.0.0 RC1 Released! – The HPX Project HPX is a general-purpose parallel C++ runtime sy…
  2. Oct 3, 2026[PLDI'26] Persistent Iterators with Value Semantics https://www.youtube.com/watch?v=cq33C5…
  3. Oct 3, 2026A Cross-Toolkit Timeline Control, Wed, Oct 14, 2026, 6:00 PM https://www.meetup.com/utah-c…
  4. Oct 3, 2026Upcoming C++ User Group meetings in October https://meetingcpp.com/meetingcpp/news/items/U…
  5. Oct 3, 2026Transcoding UTF-8 to UTF-16 with replacement at gigabytes per second https://lemire.me/blo…
  6. Oct 3, 2026EDG C++ fronted now released as open source https://edgcpp.org/#transition https://redd.it…
Threads Profile ViewerView any public Threads profile without an account.Open ThreadLook →Writing with AI? Make it sound human.Metric37 rewrites AI drafts so they read naturally. Free AI detector, 1,500 words free.Try Metric37 →