https://gcc.gnu.org/bugzilla/show_bug.cgi?id=82165
Bug ID: 82165
Summary: Operator overloading doesn't work with enum bit fields
Product: gcc
Version: 7.2.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: ivan.pogrebnyak at gmail dot com
Target Milestone: ---
Here's a short test program that illustrates the issue:
#include
#define TEST(var) std::cout << #var " = " << var << std::endl;
struct flags {
enum field { f0, f1, no_field };
field b0 : 4;
field b1 : 4;
field a0, a1;
};
inline bool operator!(flags::field f) {
std::cout << "(!) ";
return f == flags::no_field;
}
int main() {
flags f;
f.b0 = flags::f0;
f.b1 = flags::f1;
f.a0 = flags::f0;
f.a1 = flags::f1;
std::cout << " test enum values" << std::endl;
TEST( flags::f0 ) // 0
TEST( flags::f1 ) // 1
TEST( flags::no_field ) // 2
TEST( !flags::f0 ) // (!) 0
TEST( !flags::f1 ) // (!) 0
TEST( !flags::no_field ) // (!) 1
std::cout << "\n test regular members" << std::endl;
TEST( f.a0 ) // 0
TEST( f.a1 ) // 1
TEST( !f.a0 ) // (!) 0
TEST( !f.a1 ) // (!) 0
std::cout << "\n test bit field members" << std::endl;
TEST( f.b0 ) // 0
TEST( f.b1 ) // 1
TEST( !f.b0 ) // expected "(!) 0", but got "1"
TEST( !f.b1 ) // expected "(!) 0", but got "0"
}
As can be seen from the output of the tests with bit field members, the "(!)"
is not printed, and the integral values are negated, rather then compared to
the `flags::no_field`.
This doesn't appear to be a recently introduced bug, as I got the same behavior
using GCC 7.2.0, 6.3.0, 5.4.0, and 4.8.1.