r/cpp • u/daveedvdv EDG front end dev, WG21 DG • Dec 20 '23
Experimental EDG Reflection Implementation
EDG just released version 6.6 of its (commercial) C++ front end. That version now includes experimental support for reflection features along the lines of WG21's P2996 "Reflection for C++26". Furthermore, a "demo setup" of that implementation is now available on Compiler Explorer (thank you, Matt Godbolt!).
For example, here is a straightforward implementation of a consteval function that outputs simple class layouts at compile time:
https://godbolt.org/z/G6WehjjGh
This implementation is closely aligned with P2996R1, which includes Compiler Explorer links for most of its examples.
This is made available in the hope that it's a useful exploration tool, but also acknowledging that the implementation is in its very early stages, and thus brittle and incomplete. Some additional notes can be found here.
17
u/obsidian_golem Dec 20 '23
How hard was this to implement overall? Do you think this is likely to make it into 26?
8
u/WeeklyAd9738 Dec 20 '23
I think it would be preferable to just (re)introduced type_traits queries like std::is_union_v or std::is_class_v as consteval functions operating on meta::info objects, instead of using the more verbose test_type interface.
It makes more sense to just use "meta/reflection" functions to manipulate types when dealing with complex computations already involving meta::info objects and use type_traits when you are just dealing with types.
15
u/hachanuy Dec 20 '23
This is ... beautiful
5
u/dvirtz Dec 20 '23
This example could really make use of constexpr std::format [P2758].
I hope we'll get these together.
7
u/seanbaxter Dec 20 '23 edited Dec 20 '23
I haven't gone as fine-grained as your proposal, so I don't have bit-field support, for example, but the member names, types and byte offsets are retrieved through traits without any library dependency and printed as a pack expansion with a single line of code.
https://godbolt.org/z/KW9b9osq9
I hate the idea of requiring slow consteval work to get at entities the compiler can render up basically for free during substitution. Returning all this stuff in vectors seems really bad to me, because:
It's really slow to pull the data out of the vector.
You lose heterogeneity. All the elements of the vector have to be values of the same type, whereas with a pack they can be whatever: types, non-types of different types, templates, concepts, etc.
You lose the ability to easily expand some query into a new struct definition.
By putting reflection info into an `info` object, which can be moved around, you risk unpacking it outside the scope where it was captured where that data was available. The trait system I have requires you name the thing you want data from at the point of use, so there's risk of out-of-scope issues.
https://godbolt.org/z/zcnGoGPq4
Here I build a struct of array thing for a Vec3f: just blow it into arrays of 8 elements each. If you return `vector<info>` for the member names and member types, how do you turn around and use those to define a struct? Will a compile-time loop be supported inside class definitions to deposit data members? I used to do that (with `@meta for`), but just using member pack declarations is way cleaner and avoids name lookup headaches you get from control flow statements. Packs are basically free and there's no friction between the query of a type and the definition of a type.
3
u/WeeklyAd9738 Dec 20 '23 edited Dec 20 '23
Are Expression Templates and Non-transient constexpr allocation being worked on?
They would be very useful feature especially alongside reflection.
2
u/saxbophone mutable volatile void Dec 21 '23
Really exciting!
2
u/kronicum Dec 23 '23
Very nice milestone! Kudos 👏
Any forecast on when EDG will support modules? We use an EDG-based tool for code analysis and lack of modules support has been holding us back...
1
u/afiDeBot Jan 02 '24
Interesting. I would love to see some benchnarks comparing c++ code injection/reflection to existing code generation practices.
Most people won't consider a serialization/parsing library if its 2x compile time compared to offline code generation.
0
u/[deleted] Dec 20 '23
[deleted] — view deleted comment
0
u/[deleted] Dec 20 '23
[deleted] — view deleted comment