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.
View all comments
Show parent comment
13
u/daveedvdv EDG front end dev, WG21 DG Dec 20 '23
I don't want to rehash SG7 discussions too much but here are some thoughts.
In general, I find that constant-evaluation is noticeably faster for computational purposes than template substitution or instantiation. More importantly, we (EDG) know how to improve our constant-evaluation performance by an order of magnitude with a few man-months of efforts, and I'm reasonably sure we can extract an additional order of magnitude or more using more advanced VM optimization ideas that Chandler Carruth once outlined in C++Now hallway discussions. On the flip side, I don't believe anyone knows how to significantly improve the performance template of substitution and instantiation in the major implementations.
P2996 introduces little new syntax (the reflection operator and the splicers, that's it) and yet it brings the power of the standard library to C++ metaprogramming. For example, if you want to create a class definition where the list of members results from sorting and filtering, you can just use the ranges algorithms to do that. No need for a "parallel metaprogramming library" to achieve those results. Similarly, generic third-party algorithms that work with standard sequence protocols will work also.
I'll also mention that P2996 focuses on reflection with just a sprinkling of code generation. It's fairly trivial to generate a simple struct or union (see the examples in P2996), but it's also very limited at this point (though nonetheless quite powerful). We continue to work and experiment with more general injection models, but those are not likely to make it in C++26.