Re:Fun with C++26 reflection: Keyword Arguments
by harry8 on Tuesday February 11, 2025 @02:38AM
(#43010061)
>I think the important thing is just being able to serialize and deserialize POD structs to and from some representation (e.g. json). For more advanced data, such as images or specific binary (file) formats, it’s easier to write custom writing / reading, encoding / decoding logic.
Why not just use a visitor? (Genuine question, not c++ snark).
Re:Fun with C++26 reflection: Keyword Arguments
by vouwfietsman on Tuesday February 11, 2025 @05:16AM
(#43011036)
Takes a bunch of manual work per POD, that's the gist of it.
Fundamentally, if I have struct { int x = 3; std::string y = "bla"; }
I want the json to look like { x: 3, y: "bla" } without writing any code specific to the struct, and have it work bidirectionally. This is currently not possible because of a lot of reasons, but most trivially because the names "x" and "y" do not even exist anymore in your compiled code.
Re:Fun with C++26 reflection: Keyword Arguments
by Tsche on Tuesday February 11, 2025 @04:19PM
(#43018513)
This is absolutely possible, even currently - albeit in a very much non-portable way. For example boost::pfr and my own (wip) repr library have the required machinery for this.
Re:Fun with C++26 reflection: Keyword Arguments
by vouwfietsman on Thursday February 13, 2025 @05:11AM
(#43034498)
Interesting! There seem to be a lot of limitations though:
> Boost.PFR library works with types that satisfy the requirements of SimpleAggregate: aggregate types without base classes, const fields, references, or C arrays:
And in general seems to be dependent on C++20 for getting field names.
Do you know how this works? Initializer lists seem somehow involved.
So I do think you’re right that reflection can (and will) be abused by beginners if present as a language feature.
With that said, I don’t know much about the internals of the C++ compiler, but having built a simple reflection system for C++, I think the important thing is just being able to serialize and deserialize POD structs to and from some representation (e.g. json). For more advanced data, such as images or specific binary (file) formats, it’s easier to write custom writing / reading, encoding / decoding logic.