c++ - Is this warning related to enum class size wrong? -
warning:
src/boardrep.h:49:12: warning: ‘boardrep::boardrep::row::<anonymous struct>::a’ small hold values of ‘enum class boardrep::piece’ [enabled default] piece a:2; ^
enum:
enum class piece: unsigned char { empty, white, black };
use:
union row { struct { piece a:2; piece b:2; piece c:2; piece d:2; piece e:2; piece f:2; piece g:2; piece h:2; }; unsigned short raw; };
with enum
i'd agree gcc, may have truncate that's because enum
s not separate integers , pre-processor definitions. enum class
stronger. if not strong enough assume piece
values taken integers between 0 , 2 inclusive warning makes sense. otherwise gcc being needlessly picky , might worth mailing list "look, silly warning"
incase cannot see point
you can store 4 distinct values in 2 bits of data, need 3 distinct values, enum of length 4 or less should fit nicely in 2 bits given (and enum "derive" (better term?) unsigned type). if had 5 or more i'd expect warning.
the warning issued gcc
accurate, there's no need compose mail mailing list asking them make warning less appear.
the standard says enumeration underlying type of unsigned char
cannot represented bitfield of length 2
; if there no enumerations holds such value.
the standard
the underlying value of enumeration valid if there no enum-keys corresponding value, standard says legal value stored inside enumeration must fit inside underlying type; doesn't state such value must present among enum-keys.
7.2 enumeration declarations
[dcl.enum]
7 ... possible define enumeration has values not defined of enumerators. ...
note: quoted section present in both c++11, , draft of c++14.
note: wording stating same thing, using different terminology, can found in c++03 under [dcl.enum]p6
note: entire [decl.enum]p7
hasn't been included preserve space in post.
details
enum class e : unsigned char { a, b, c }; e x = static_cast<e> (10);
above initialize x
store value 10
, if there's no enumeration-key present in enum-declaration of enum class e
still valid construct.
with above in mind deduce 10
cannot stored in bit-field of length 2
, warning gcc
nothing accurate.. potentially trying store values in our bit-field cannot represent.
example
enum class e : unsigned char { a, b, c }; struct { e value : 2; };
a val; val.value = static_cast<e> (10); // omg, ops!?
Comments
Post a Comment