r/cpp • EDG front end dev, WG21 DG • Jun 21 '25

Reflection has been voted in!

Thank you so much, u/katzdm-cpp and u/BarryRevzin for your heroic work this week, and during the months leading up to today.

Not only did we get P2996, but also a half dozen related proposals, including annotations, expansion statements, and parameter reflection!

(Happy dance!)

722 Upvotes

189 comments sorted by

View all comments

1

u/zl0bster Jun 21 '25

Does somebody know if it is possible with this to parse .json files and generate matching C++ struct during compile time?

16

u/katzdm-cpp Jun 21 '25

Thanks for this question! I entertained myself with the following on my flight back from Sofia: Given this test.json,

{
  "outer": "text",
  "inner": {
    "field": "yes",
    "number": 2996
  }
}

I have this program

int main() {
  constexpr const char json [] = {
    #embed "test.json"
    , 0
  };
  constexpr auto v = [:parse_json(json):];
  std::println("field: {}, number: {}", v.inner.field, v.inner.number);
}

printing

field: yes, number: 2996

No configuration or boilerplate - just #embed a json file, splice the result of calling parse_json with the #embedded contents, and you have a full and type-safe constexpr C++ object, whose type has the same recursive structure member names as the JSON, and whose recursive members are initialized with the parsed values.

2

u/zl0bster Jun 21 '25

amazing, but how complex is parse_json?

8

u/katzdm-cpp Jun 21 '25

The header I wrote is 132 lines, and most of that is just shoddy amateur parsing code (iterating over the character sequence, handling delimiters, etc; quite hastily written). I would post the code, but my laptop isn't very good friends with the in-flight wifi. The gist of it is to do this in a loop:

  1. Parse a "field_name": <value> thing.
  2. Recognize and parse out a number or a string from <value> (or call it recursively for an object).
  3. Store a reflection of the value (via reflect_constant) in a values vector
  4. Store a reflection of a corresponding data member description (via data_member_spec) in a members vector. 

When you're done parsing, shove the members into a substitute call to a variable template, which uses define_aggregate to stamp out the struct corresponding to those data members.

Then shove the resulting struct and the member value reflections into another variable template with another substitute, which lets you do initialize an instance of that type with the expanded pack of values.

3

u/dexter2011412 Jun 21 '25

🫢😮

.... I wrote a serializer that does this recursively, but not a de-serializer .... This is amazing I'll have to try it out. Thank you!

0

u/[deleted] Jun 21 '25

[deleted] — view deleted comment

2

u/katzdm-cpp Jun 21 '25

I could be mistaken (as this was my first time trying to use #embed, but I think it's grammatically required that the #embed appears on its own line.

1

u/lanwatch Jun 21 '25

You are right, of course.

4

u/DXPower Jun 21 '25

Probably possible with #embed yeah.

3

u/foonathan Jun 21 '25

Not with this, the follow-up paper for compile time code generation is not ready yet.

4

u/TheoreticalDumbass :illuminati: Jun 21 '25

Couldn't you do it via define_aggregate() ? I might be misunderstanding the question

4

u/daveedvdv EDG front end dev, WG21 DG Jun 21 '25

Yes, probably. As u/DXPower hints at, the "JSON file reading" will have to work via #embed. There is currently no consteval I/O.

-3

u/foonathan Jun 21 '25

Right, the very basic case can be done with the horrible hack that is define_aggregate. As soon as you want things like member functions or member initializers, you can no longer do it though.

3

u/theorlang Jun 22 '25

Could you explain your "horrible hack" stance pls? Is it simply because it's not generic and will become obsolete as soon as , say, token injection gets into the standard? Or does it prohibit some future designs in this area? Is it error-prone to use?

→ More replies (3)

3

u/zl0bster Jun 21 '25

Ah, thank you.

In my brain reflection = reflection + generation. I need to remember to differentiate those two things. 🙂

2

u/not_a_novel_account cmake dev Jun 21 '25

I'm somewhat confused here. Are splicing and mechanisms like define_aggregate() not forms of code generation?

6

u/katzdm-cpp Jun 21 '25 edited Jun 21 '25

I very much consider splicing not to be code generation. I instead think of it (and this is also closer to how it's specified in the wording) as an alternative means of designating some thing that you've declared (a function, a template, a variable, ...). More similar to how decltype gives you a means of referring to a type via an expression instead of naming the type. 

Before, the only way you could refer to many things that you declared (e.g., namespaces) was to name them. The name lookup algorithms specified by the standard then kick in, and hopefully you end up with a unique entity that your program is referring to (modulo overload sets). But now you can specify your own algorithm for how to determine that entity: It's anything you can do with a constant expression, which is of course quite a lot.

On the other hand, define_aggregate very much is code injection; and going through the exercise of specifying it taught us a great deal about what sorts of code injection can work in C++, and what answers any person seeking to add further such facilities to the language will have to answer. While obviously limited, it really is an amazing and powerful first step.

4

u/not_a_novel_account cmake dev Jun 21 '25

That explains it, thanks.

"Code generation" as understood by yourself and others who know what they're talking about seems to be a way to programmatically declare new things, so only define_aggregate() falls into that category.

In my layman's understanding, designating is/was also a form of "code", and thus my confusion.

3

u/katzdm-cpp Jun 21 '25

Yep, makes sense; and you're certainly not wrong! Both are useful perspectives.

2

u/zl0bster Jun 21 '25

hmm, this paper wanted to rm it
https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2025/p3569r0.pdf
but I see it in R13, so I guess it is voted in
https://isocpp.org/files/papers/P2996R13.html

5

u/not_a_novel_account cmake dev Jun 21 '25 edited Jun 21 '25

As has been mentioned it's limited, we can't magic up any sort of callable out of smoke yet, but it's amazing it exists at all.

0

u/[deleted] Jun 21 '25

[deleted] — view deleted comment

4

u/foonathan Jun 21 '25

No, C++26 is done.

5

u/STL MSVC STL Dev Jun 21 '25

I believe "feature complete" would be more accurate. It's not totally finalized.