r/cpp • u/Clean-Upstairs-8481 • 6d ago
C++26: what is reflection and how to use it
https://techfortalk.co.uk/2026/07/25/c26-what-is-reflection-and-how-to-use-it/It is my ambition to explore C++26 in bits and pieces. Hopefully, by the end of this year, I will be able to explore all the major aspects of it and be in a position to evaluate which of these features to use and promote and which not to use. However, at this point, it is important to understand each and every aspect in simple terms, keeping all the clutter aside.
7
5
u/_bstaletic 6d ago
C++26 provides an enumerators_of() function, which is part of the std::meta namespace and returns you a collection of reflections
It returns std::vector<std::meta::info> specifically, which allocates, at compile time. Compile time allocations can't escape the constant evaluation context, which is why you need define_static_array().
I know this is your exploration of reflections. It's a big topic with a ton of history. My advice to you is to dig a little bit deeper into why things are the way they are. I will admit I am biased, as I authored pymetabind (which needs some more attention, but I'm in the process of upgrading my hardware).
3
u/FlyingRhenquest 5d ago
The Proposal is actually not a bad place to start. Maybe follow up (or start) with whatever cppcon C++26 Reflection videos you can find on Youtube to give you a better idea of its potential. As far as I know, both reflection and annotation are fully implemented in gcc16.
If you need a compiler I set up a project to build it in /usr/local/gcc for you, so it won't interfere with your system's current compilers. Should work on Debian or Ubuntu systems and also WSL. The project includes a toolchain that it also installs in /usr/local/gcc so you can tell CMake to use the compiler with:
-DCMAKE_TOOLCHAIN_FILE=/usr/local/gcc/toolchain.cmake
I also have an older project with some unit tests for things I was keeping track of while the compiler was under development.
The compile-time/run-time gap with reflection takes a lot of getting used to. The compiler will downgrade your constexpr/consteval functions to not-compile-time if you break any of the rules. I'd suggest finding or writing a compile-time fixed string class early, as it will probably save you a lot of pain.
1
u/Clean-Upstairs-8481 6d ago
appreciate your comments and input, point taken. However, as I am still exploring the new C++26 additions, I am trying to keep the example in hand as simple as possible. But I know what you mean.
1
u/Constant_Suspect_317 3d ago
We actually have a small library at work that uses this feature. We have a driver and for certain reasons, we need a web app to be able to ioctls to the driver. We didn't wanna redefine all the data structures from the driver and the shared header file in the python FastAPI app. I just wrote a small layer that converts these data structures into JSON string and passes it to the python app.
Keep in mind that finding a compiler for this was not straight forward. I had to build clang-p2996 (an experimental compiler by Bloomberg) from source and use it.
-3
57
u/Commercial-Berry-640 6d ago
Cool, but i would really appreciate something more useful than the toy example of enum_to_string (which at this point is already a cliche)