当分配1时,这个大小为1的位字段实际上是溢出的吗?

FSM*_*axB 2 c gcc integer-overflow compiler-warnings

我已经编写了一些代码字段的代码,我认为应该可行,但看起来像GCC不同意.我是否错过了某些内容或者我是否真的在GCC中发现了一个错误?

简化我的代码后,测试用例非常简单.我将整数文字分配给1一个大小为一位的位域:

typedef struct bitfield
{
    int bit : 1;
} bitfield;

bitfield test()
{
    bitfield field = {1};
    return field;
}
Run Code Online (Sandbox Code Playgroud)

如果我使用GCC 6.2.1(与5.4.0相同)编译它,我会收到以下警告(使用-pedantic):

gcc -fPIC test.c -pedantic -shared
test.c: In function ‘test’:
test.c:8:23: warning: overflow in implicit constant conversion [-Woverflow]
     bitfield field = {1};
                       ^
Run Code Online (Sandbox Code Playgroud)

奇怪的是:当我用-Woverflow替换-pedantic时,警告消失了.

我没有得到任何警告警告.

Bar*_*mar 9

使用unsigned int此位字段.1位有符号数只能保持0-1.

typedef struct bitfield
{
    unsigned int bit : 1;
} bitfield;
Run Code Online (Sandbox Code Playgroud)