我使用以下联合来简化字节、半字节和位操作:
union Byte
{
struct {
unsigned int bit_0: 1;
unsigned int bit_1: 1;
unsigned int bit_2: 1;
unsigned int bit_3: 1;
unsigned int bit_4: 1;
unsigned int bit_5: 1;
unsigned int bit_6: 1;
unsigned int bit_7: 1;
};
struct {
unsigned int nibble_0: 4;
unsigned int nibble_1: 4;
};
unsigned char byte;
};
它工作得很好,但它也会生成此警告:
警告:ISO C++ 禁止匿名结构 [-pedantic]
好的,很高兴知道。但是...如何从我的 g++ 输出中获取此警告?是否有可能在没有这个问题的情况下编写类似这个联合的东西?
gcc 编译器选项-fms-extensions http://gcc.gnu.org/onlinedocs/gcc-3.4.4/gcc/C-Dialect-Options.html将允许非标准匿名结构而不发出警告。
(该选项启用了它所认为的“微软扩展”)
你也可以达到同样的效果有效的 C++使用这个约定。
union Byte
{
struct bits_type {
unsigned int _0: 1;
unsigned int _1: 1;
unsigned int _2: 1;
unsigned int _3: 1;
unsigned int _4: 1;
unsigned int _5: 1;
unsigned int _6: 1;
unsigned int _7: 1;
} bit;
struct nibbles_type {
unsigned int _0: 4;
unsigned int _1: 4;
} nibble;
unsigned char byte;
};
有了这个,您的非标准byte.nibble_0
成为合法的byte.nibble._0
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)