I try to get the size of a bitfield.
For example, I have got a generic handle:
template<size_t n, size_t m>
struct handle
{
uint32 index : n;
uint32 validation : m;
}
Now I want to get the size of the members.
I found a macro that works when I have a handle<16, 16>
and expands the desired members to sizeof
. In this case, if I pass in the index
members I get 16
as my output.
But there I would have to pass in my output variable.
Is there a way maybe with some template magic to expand directly to the desired number? So I could pass in sizeof_bit(class, member)
and I get the sizebit
size of this member?
Maybe something like
template<size_t n, size_t m>
struct handle
{
enum { index_bits = n };
enum { validation_bits = m };
uint32_t index : n;
uint32_t validation : m;
};
Demo .
If you have no control over the structure defining the bitfield, you can do something like this to count the number of bits in it at run time, but it's fairly inefficient.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.