问题描述
我使用以下联合来简化字节,半字节和位操作:
I use the following union to simplify byte, nibble and bit operations:
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;
};
效果很好,但还会生成此警告:
It works nice, but it also generates this warning:
好的,很高兴知道.但是...如何从我的g ++输出中得到这个警告?如果没有这个问题,是否有可能写这样的联合体?
Ok, nice to know. But... how to get this warning out of my g++ output? Is there a possibility to write something like this union without this issue?
推荐答案
gcc编译器选项 -fms-extensions
将允许非标准的匿名结构,而不会发出警告.
The gcc compiler option -fms-extensions
will allow non-standard anonymous structs without warning.
(启用了它认为的"Microsoft扩展")
(That enables what it considers "Microsoft extensions")
使用此约定,您还可以在有效的C ++ 中实现相同的效果.
You can also achieve the same effect in valid C++ using this convention.
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
With this, your non-standard byte.nibble_0
becomes the legal byte.nibble._0
这篇关于C ++匿名结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!