TheNitesWhoSay / RareCpp

Creating a simpler, more intuitive means of C++ reflection
MIT License
124 stars 6 forks source link

Auto Aggregate Reflection #116

Closed TheNitesWhoSay closed 11 months ago

TheNitesWhoSay commented 11 months ago

As of C++20 it's possible to auto-magically get the names of the members of aggregates on all major compilers (in addition to auto-magically getting the values and types - which was already possible); integrate support for auto-reflecting aggregates when on C++20 & using them with the regular RareCpp interfaces.

TheNitesWhoSay commented 11 months ago

Auto-reflection of aggregates was released in 2.3.0, see documentation

This is only available when compiling against C++20 or higher and has many limitations that the macros do not as described here.

example1: https://godbolt.org/z/K75dhj5K4 example2: https://godbolt.org/z/GcKharfxj

struct Item
{
    int id = 0;
    std::string name {};
};

struct Collection
{
    std::string description {};
    std::vector<Item> items {};
};

int main()
{
    Collection collection { "my collection", {{0, "first"}, {1, "second"}} };
    std::cout << Json::pretty(collection);
}

Output:

{
  "description": "my collection",
  "items": [
    { "id": 0, "name": "first" },
    { "id": 1, "name": "second" }
  ]
}