Reputation: 6010
#include <stdint.h>
enum state : uint8_t {
NONE,
USA,
CAN,
MEX
};
struct X {
state st : 2; // compiles with uint8_t st : 2
};
Clang 3.9.0 compiles successfully.
GCC 4.8.4 and 5.3.0 complain with:
warning: ‘X::st’ is too small to hold all values of ‘enum state’
Who is right?
Upvotes: 4
Views: 519
Reputation: 70263
TL;DR
Both are correct.
The value of an enumeration is limited by the underlying type, not by the enumerators!
C++14, 7.2 Enumeration declarations, paragraph 8:
It is possible to define an enumeration that has values not defined by any of its enumerators.
Which means it is possible to:
state x = static_cast< state >(5);
That is what GCC is warning you about: enum state
could have values that do not fit into 2 bits.
However, as long as you don't try to actually do that to X::st
, everything is shiny.
That's (probably) why Clang is not warning you about it.
Since the standard does not demand a diagnostic either way, there's nothing wrong about warning, or not warning you.
Upvotes: 1