Why is std::bitset<8> 4 bytes big?

前端 未结 7 2105
滥情空心
滥情空心 2021-01-18 17:54

It seems for std::bitset<1 to 32>, the size is set to 4 bytes. For sizes 33 to 64, it jumps straight up to 8 bytes. There can\'t be any overhead because std::bitset<

7条回答
  •  悲哀的现实
    2021-01-18 18:32

    If your std::bitset< 8 > was a member of a structure, you might have this:

    struct A
    {
      std::bitset< 8 > mask;
      void * pointerToSomething;
    }
    

    If bitset<8> was stored in one byte (and the structure packed on 1-byte boundaries) then the pointer following it in the structure would be unaligned, which would be A Bad Thing. The only time when it would be safe and useful to have a bitset<8> stored in one byte would be if it was in a packed structure and followed by some other one-byte fields with which it could be packed together. I guess this is too narrow a use case for it to be worthwhile providing a library implementation.

    Basically, in your octree, a single byte bitset would only be useful if it was followed in a packed structure by another one to three single-byte members. Otherwise, it would have to be padded to four bytes anyway (on a 32-bit machine) to ensure that the following variable was word-aligned.

提交回复
热议问题