Does it have to be C++26 reflection? :) 'Cause I use Unreal Engine-style reflection in some of my personal projects, via a custom reflection tool. I use it for serialization, debugging, editor introspection, garbage collection, script binding, RPCs/network hookup, creating state machines, ORM creation, making dependency injection easier, and documentation generation.
Sounds very interesting. Care to elaborate more? Im fairly new to c++ and I'm always running into better practises, libraries, gotchas, etc everday, hah
if you’re new to c++, in my opinion, you shouldn’t even look at the reflection features yet. There are few good use cases for it. If you’re not familiar enough with the more mature language features yet, it can be easy to overuse reflection.
Though it depends on how “new” you are to c++. Also, again, just my personal opinion.
Keeping in mind that Unreal Engine is pretty non-idiomatic, you can read up on how Unreal Engine 5 does reflection. Mine is similar, though I designed mine mostly separately (have been working on this idea for over a decade).
In the MUD project, I use reflection for serialization, garbage collection, script-binding, user-help generation, and enum-to-string functionality, plus most likely other things I forgot :)
By custom tool do you mean something you made? Would be interested in more details and implementing something like it myself if it doesn’t turn out to be too much trouble!
Autocereal builds on cereal, using reflection to eliminate the need to write load/save boilerplate functions for serialization. Also includes a JSON schema generator and convenience functions to serialize/deserialze to strings and streams.
Autocrud implements a CRUD database object that can create tables for C++ objects and store and load data from those tables. Uses Postgres (pqxx).
I also put up a project with scripts to build the latest gcc and cmake from their git repos and provides a CMake toolchain file for people who want to play with gcc16 before their OS provides packages for it. I've tested it on my Debian laptop and think it should work on WSL as well. It might even work with OSX.
And like savages, we have to write this out or wrap it ourselves, unlike the glorious land of shader languages, which provide an intrinsic for it (granted, it's probably much more frequently used there).
Yeah, even outside graphics, I've found myself missing many of the intrinsics from HLSL/Slang/GLSL, like lerp (which we pleasantly got in C++20 as std::lerp), normalize, length, distance, dot (std::inner_product is close, but wordier than just passing two parameters of the same rank - imagine a dot function that took any two range-compatible inputs of the same rank, resolvable at compile time, like 2 std::array's), and reversebits. All these have utility beyond drawing scenarios, and I never needed std to have an entire 2D drawing API that would be quickly obviated while also adding high burden onto implementers, but I have often lacked the basic building blocks that a drawing API might use.
You guys ever end up using std::lerp and wishing there was an inverse/unlerp() function?
I cant imagine why they would standardize lerp but not unlerp. I find that 90% of my uses of one involve the other (i.e to remap a range, usually colors or noise values or something like that).
(of course its trivial to hand roll, but its just a bit awkward using my_own::unlerp() alongside std::lerp())
I haven't needed unlerp ((x - a) / (b - a)), because generally my range minimum is 0 when normalizing (reducing that to just x / b), but I see how it could be useful, and evidently it exists in Unity and Construct.
I've been doing a bunch of stuff with reflection, but the most recent thing is this proof of concept that pulls out bits from some trivially copyable thing: https://godbolt.org/z/qMjYTnMo7
const auto my_bits = MyBits(std::uint16_t{/* ... */});
And access the bits like my_bits.epic_bits and so on.
The proof of concept uses std::bit_cast using a dynamically-created bit-field aggregate, so it does rely on implementation-defined stuff, but it should definitely be possible to manually pull out the bits and not rely on anything implementation-defined. I was just lazy for the proof of concept (and the code could definitely be improved some too).
I should also say that this was inspired by some Rust crate I happened to see a long while ago, but I couldn't for the life of me remember the name of it and I had trouble finding out which crate it was.
I implemented a struct serialization/deserialization for my custom JSON class: Nebulite JsonScope . Supports nested structs, arrays and basic values like int, std::string, double, etc. Was way easier than expected; about 150 loc, not including the JSON api itself ofc.
I tested my gcc playground project on a WSL ubuntu 22.04 image and it built the compiler just fine. So if you want a head start on the gcc c++26 goodies, you can kind of get that on Windows via WSL. My project sets up CMake toolchain file and doesn't interfere with other compilers installed on the system, which should allow for an easy transition for experimenting with the features with gcc to just not using the toolchain and building with your usual compilers whenever they get around to adding support for reflection and all those other goodies.
Of course, once you get used to reflection you might find yourself lamenting the lack of compile time cheese on your usual compilers. You can achieve some level of that with typelists in earlier versions of C++, though it's not as flexible or easy to use. Still not a bad tool to have in your back pocket if you want to eliminate massive blocks of switch statements to do the same thing with 80 different types.
18
u/Kronikarz May 03 '26
Does it have to be C++26 reflection? :) 'Cause I use Unreal Engine-style reflection in some of my personal projects, via a custom reflection tool. I use it for serialization, debugging, editor introspection, garbage collection, script binding, RPCs/network hookup, creating state machines, ORM creation, making dependency injection easier, and documentation generation.
19
u/FlyingRhenquest May 02 '26
I've got a couple.
Autocereal builds on cereal, using reflection to eliminate the need to write load/save boilerplate functions for serialization. Also includes a JSON schema generator and convenience functions to serialize/deserialze to strings and streams.
Autocrud implements a CRUD database object that can create tables for C++ objects and store and load data from those tables. Uses Postgres (pqxx).
I also put up a project with scripts to build the latest gcc and cmake from their git repos and provides a CMake toolchain file for people who want to play with gcc16 before their OS provides packages for it. I've tested it on my Debian laptop and think it should work on WSL as well. It might even work with OSX.
48
u/fdwr fdwr@github 🔍 May 03 '26
R = V - 2 * N * dot(V, N)is the most common usage in my C++ projects. I will see myself out...12
u/friedkeenan May 03 '26
I've been doing a bunch of stuff with reflection, but the most recent thing is this proof of concept that pulls out bits from some trivially copyable thing: https://godbolt.org/z/qMjYTnMo7
You can define a type like
And then pull out the bits like
And access the bits like
my_bits.epic_bitsand so on.The proof of concept uses
std::bit_castusing a dynamically-created bit-field aggregate, so it does rely on implementation-defined stuff, but it should definitely be possible to manually pull out the bits and not rely on anything implementation-defined. I was just lazy for the proof of concept (and the code could definitely be improved some too).I should also say that this was inspired by some Rust crate I happened to see a long while ago, but I couldn't for the life of me remember the name of it and I had trouble finding out which crate it was.
6
u/G6L20 May 03 '26
https://github.com/Garcia6l20/reflex
2
u/VomAdminEditiert May 05 '26
I implemented a struct serialization/deserialization for my custom JSON class: Nebulite JsonScope . Supports nested structs, arrays and basic values like int, std::string, double, etc. Was way easier than expected; about 150 loc, not including the JSON api itself ofc.
1
u/dexter2011412 May 03 '26
nice try ai /s
1
u/Huge-Presentation810 May 21 '26
Nested and dynamically evaluated yaml config loader:
-4
u/pjmlp May 03 '26
My projects are cross platform, using pre installed compilers, and at home, I use mainly Visual C++, thus no reflection for the foreseeable future.