I wanted to have a struct that can be accessed both as an array and as named members. I wrote this:
struct RangeLimits
{
enum class RangePenalty: byte
{
OUT_OF_RANGE,
NO_PENALTY,
SHORT,
MEDIUM,
LONG,
EXTREME
};
union
{
struct
{
range shrt;
range med;
range lon;
range ext;
};
std::array<range, 4> arang;
};
};
namespace static_test
{
constexpr static bool checkThatArrayMapsToFields()
{
RangeLimits limits{ range{1},range{1},range{1},range{1} };
limits.med = range{ 66 };
return limits.arang[1] == range{ 66 };
}
static_assert(checkThatArrayMapsToFields(), "Union not behaving as expected");
range
is really just unsigned char. I am getting this error:
1>RangeLimits.h(41,41): error C2131: expression did not evaluate to a constant
1>RangeLimits.h(38,16): message : failure was caused by accessing a non-active member of a union
1>RangeLimits.h(38,16): message : see usage of 'techsheet::RangeLimits::arang'
I do understand this is how unions are supposed to behave in C++ (well, now I do anyway). But I do not understand why does C++ care, why this rule even exists. After all, you can't always check this anyway.