3

there is a class declared as:

class Payload {
    uint8_t m_a : 1;
    uint8_t m_b : 5;
    uint8_t m_c : 2;
    uint8_t m_d : 4;
};

you see, the sizeof(Payload) == 2. So far so good. The total amount of bits is 12, it exceeds the 8bit thus 2Bytes are occupied.

But is there a way how to get the amount of bits the class declares? I mean to return '12'...? Sure to make the algorithm working for any kind of class definining bitfields as above, theoreticaly of any size...

Martin Kopecký
  • 912
  • 5
  • 16
  • I don't think so... (until reflection maybe). – Jarod42 Dec 05 '20 at 20:32
  • @πάνταῥεῖ That' s pretty clear but I was not asking for how many Bytes it occupies, thus sizeof( Payload ) but the amount of BITS - to programatically detect the bitwidth, the 12 in this particular example – Martin Kopecký Dec 05 '20 at 20:33
  • Why do you need to know that the number of bits is 12 (assuming that's what you mean)? Sounds like an xy problem. – G.M. Dec 05 '20 at 20:33
  • I don't think we can know regular bytes padding currently, so bits padding... – Jarod42 Dec 05 '20 at 20:35
  • Most processors access data in bytes. For this reason also C and C++ measure structures in bytes (actually chars). – rustyx Dec 05 '20 at 20:36
  • @G.M. I am implementing a tagged_ptr, which uses the 'unused bits' thanks ot pointer alignment to store a payload. I can calculate how many bits are effectively unused by the pointer itself, e.g. allignment=1024, n_bits=10 etc and I wan to check whether my payload class with its 'bits' fits to the dimension of free to use bits of the aligned pointer, you know... – Martin Kopecký Dec 05 '20 at 20:37
  • @MartinKopecký A type with bitfield members is considered to consume all bits of the bytes it occupies even padding bits. It is not possible to determine which of its bits are unused. – François Andrieux Dec 05 '20 at 20:38
  • @FrançoisAndrieux : Sure but can the 'active' bits be counted somehow? – Martin Kopecký Dec 05 '20 at 20:40
  • @MartinKopecký No. C++ doesn't support what your trying to do. – François Andrieux Dec 05 '20 at 20:41
  • Maybe you can use https://stackoverflow.com/questions/4908300/overflow-in-bit-fields and compute the size based on maximal values that can be stored within the class members? – Martin Perry Dec 05 '20 at 20:46
  • @MartinPerry How overflowing for bitfields works is implementation defined, even for `unsigned` integer types. So you cannot rely on that. Maybe it works in C, but it does not in C++. – François Andrieux Dec 05 '20 at 20:50
  • So maybe this solution - https://stackoverflow.com/questions/20194009/size-of-a-bitfield-member. But again, may depends on compiler, so not entirely "portable" – Martin Perry Dec 05 '20 at 21:06

2 Answers2

1

sizeof(Payload)*CHAR_BIT. This gets the size of the structure in bytes and multiplies it by the number of bits per byte (it’s technically not always 8). This works because bitfield-containing structs cannot have a size in bits which is not a multiple of CHAR_BIT. The compiler will add padding bits after the last member.

Sneftel
  • 40,271
  • 12
  • 71
  • 104
1

No, not in a standard-conformant way.

[class.bit]/1:

the bit-field attribute is not part of the type of the class member

which means this information cannot be extracted at compile time. Any kind of attempt to find this information using runtime introspection techniques would rely on, at best, implementation defined behaviour.

dfrib
  • 70,367
  • 12
  • 127
  • 192