Closed
Description
In C++, signed integers are represented in two's complement. This also applies to signed bitfields.
A signed bitfield composed of one bit can therefore store a value in the range -1 to 0.
Assigning a value of 1 to such a bitfield should produce a warning since it is out of range of representable values.
MSVC and GCC generate the correct warnings for this situation, clang does not.
clang 13.0.0
-std=c++20 -Wall -Wextra -Wpedantic -Wconversion -Wbitfield-constant-conversion
struct test
{
signed int v : 1;
};
int main()
{
test t;
// MSVC: warning C4463: overflow; assigning 1 to bit-field that can only hold values from -1 to 0
// GCC: overflow in conversion from 'int' to 'signed char:1' changes value from '1' to '-1' [-Woverflow]
// clang: no warning produced
t.v = 1;
return t.v; // Returns -1
}