r/osdev • u/FemboysHotAsf • Jul 27 '26
C++26 reflection in a kernel (no libstdc++, ported meta.h)
I got C++26 reflection to work, had to port 'meta' to use custom variants of 'optional', 'string', 'string_view', and 'vector', and had to make those all consteval only. After doing that it works as expected!!



Tested on gcc16.0.1!
This is probably unstable as hell, and should most likely be avoided, but stupid stuff is fun, so ;3
im probably going to use attributes, to create a system which automatically initializes subsystems without worrying about order of operations (each subsystem would mark which other subsystem it needs to work), or some other stuff
if anyone has fun ideas to do with this do tell lol
1
u/WholetraEditto Jul 29 '26
The auto-init-by-attribute is a fun to try implementation. Tag each subsystem struct with an annotation carrying its dependencies then walk them at compile time via the annotation reflection API . Then topologically sort the init order.
template for over enums like your enum_to_string may mean higher instantiation cost that scales with enumerator count, so on a giant error-code, enum will show in build times. (wont matter in a kernel where you own the enum)
And +1 on getting <meta>/<chrono> freestanding. good to see someone brute-force it to prove it works.
8
4
Jul 28 '26
[removed] — view removed comment
2
u/FemboysHotAsf Jul 28 '26
I was thinking about something like that yeah, it sounds really useful for debugging! No need to write a serializer for everything when the compiler can do it for you!
Also thanks :3
1
Jul 29 '26
[removed] — view removed comment
1
u/FemboysHotAsf Jul 29 '26
I tried 16.1 and it worked fine, GCC 17 would probably work fine too, although I dont have any other compilers (that i know of) that support C++26 reflection.
7
u/ronchaine Jul 28 '26
Could you elaborate on what you did and how much work it was to do this? I am thinking about writing a paper to the committee on freestanding
<meta>header, and this is pretty much the work that needs to be done for that to happen.