Я только что прошел тест с битполями, и результаты меня удивляют.C++ bitfield упаковка с bools
class test1 {
public:
bool test_a:1;
bool test_b:1;
bool test_c:1;
bool test_d:1;
bool test_e:1;
bool test_f:1;
bool test_g:1;
bool test_h:1;
};
class test2 {
public:
int test_a:1;
int test_b:1;
int test_c:1;
int test_d:1;
int test_e:1;
int test_f:1;
int test_g:1;
int test_h:1;
};
class test3 {
public:
int test_a:1;
bool test_b:1;
int test_c:1;
bool test_d:1;
int test_e:1;
bool test_f:1;
int test_g:1;
bool test_h:1;
};
Результаты были: -
sizeof(test1) = 1 // This is what I'd expect. 8 bits in a byte
sizeof(test2) = 4 // Reasonable. Maybe padded out to the size of an int.
sizeof(test3) = 16 // What???
Это то, что вы ожидаете, или ошибка компилятора? (Codegear C++ Builder 2007, btw ...)
Если вы хотите получить больше контроля над компоновкой структур битового поля в памяти, рассмотрите возможность использования этого объекта бит-поля, реализованного в виде файла заголовка библиотеки: [link] (https://github.com/wkaras/C- plus-plus-library-bit-fields/blob/master/Bitfield.pdf) – WaltK 2017-01-04 18:27:37