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.
View all comments
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.