[Bug c++/51242] [C++11] Unable to use strongly typed enums as bit fields
tom at honermann dot net
gcc-bugzilla@gcc.gnu.org
Tue Jun 14 18:20:00 GMT 2016
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=51242
Tom Honermann <tom at honermann dot net> changed:
What |Removed |Added
----------------------------------------------------------------------------
CC| |tom at honermann dot net
--- Comment #27 from Tom Honermann <tom at honermann dot net> ---
We got bit by this warning recently. We compile with -Werror and, without an
option to suppress warnings in the following code, gcc rejects it. We think
this code should be accepted without a warning given the constant expressions
involved.
Clang does not issue a warning for this code. However, if an assignment to the
bit-field from a literal '2' (cast to E) is added, then Clang warns on the
assignment itself (as opposed to on the bit-field declaration). This seems
like a more useful approach.
$ cat t.cpp
enum E : unsigned char {
e0 = 0,
e1 = 1
};
struct S {
E e : 1;
};
void f(S s) {
s.e = e0;
s.e = e1;
s.e = (E)0;
s.e = (E)1;
};
$ g++ --version
g++ (GCC) 6.1.1 20160531
...
$ g++ -c -std=c++11 -Werror t.cpp
t.cpp:6:11: error: ‘S::e’ is too small to hold all values of ‘enum E’ [-Werror]
E e : 1;
^
cc1plus: all warnings being treated as errors
More information about the Gcc-bugs
mailing list