有一个类声明为:
class Payload {
uint8_t m_a : 1;
uint8_t m_b : 5;
uint8_t m_c : 2;
uint8_t m_d : 4;
};
你看,sizeof(Payload) == 2
。到目前为止,一切都很好。总bit数为12,超过8bit,占用2Bytes。
但是有没有办法获取类声明的位数?我的意思是返回“12”...?一定要使算法适用于任何类型的类定义位域,理论上任何大小...
最佳答案
不,不是以符合标准的方式。
the bit-field attribute is not part of the type of the class member
这意味着这些信息无法在编译时提取。任何使用运行时自省(introspection)技术查找此信息的尝试最多都依赖于实现定义的行为。
关于c++ - 类的位宽,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65161639/