logoalt Hacker News

maccardyesterday at 2:08 PM2 repliesview on HN

Serialization is the canonical example. Being able to turn

    struct MyStruct {
      int val = 42;
      string name = "my name";
    };
    
into

    {
      "val": 42, // if JSON had integers, and comments of course
      "name": "my name",
    }
is incredibly powerfuly. If reflection supported attributes (i can't believe it shipped without, honestly), then you could also mark members as [[ignore]] and skip them.

Replies

SuperV1234yesterday at 2:47 PM

You can achieve that since C++20 (or C++17 if you don't care about the member names). E.g. https://www.linkedin.com/posts/vittorioromeo_cpp-gamedev-ref...

(The link above shows ImGui generation, but the same exact logic can be applied for serialiation to JSON/YAML/whatever.)

show 1 reply
bluGillyesterday at 2:25 PM

It is powerful, but I'm not sure it is a good idea. Other languages have it, and there is lots of experience in all the ways things go wrong in the real world. I'm inclined to say you should hand write this code because eventually you will discover something weird anyway.

show 3 replies