Reputation: 1897
In C++14, I would like to restrict the total number of bits held by an enum class:
enum class InstalledCapacity : uint8_t{
None,
One_1000uF,
Two_1000uF,
Three_1000uF,
One_1F,
One_3_3F,
Reserved2,
Invalid
};
using HarvestingCapability = uint8_t;
typedef struct {
InstalledCapacity installed_capacity : 3;
HarvestingCapability harversting_capability_x_15mW : 5;
}EnergyInfo;
This does not seem to work, and I get the following warning:
eeprom_metadata.h:51:42: warning: '<anonymous struct>::installed_capacity' is too small to hold all values of 'enum class InstalledCapacity'
InstalledCapacity installed_capacity : 3;
^
Since I only have 7 values in my InstalledCapacity
enum class, I would expect to be able to only use 3 bits for it.
What am I doing wrong, is this even possible? Many thanks in advance!
Upvotes: 4
Views: 764
Reputation: 2648
Nothing is wrong, the compiler is just commenting that 3 bits is too small to hold all possible values of the enum, which can be as large as 8 bits. Just because all of your named enumeration values can fit into 3 bits doesn't mean that all the possible values of InstalledCapacity
can fit into the bitset. A value of 255
is perfectly valid for the enum, but cannot fit into your bitset.
Upvotes: 5