Having referenced this question: Can an enum class be converted to the underlying type?.
In my code I have effectively:
enum class STATE : short
{
EMPTY,
PRESENT,
PARTIAL,
};
volatile STATE state;
Then I write a typedef and a static_assert:
typedef volatile std::underlying_type<STATE> state_type;
static_assert (sizeof (state_type) == sizeof (short), "Error - unsafe for use with InterlockedCompareExchange16");
Finally I attempt to set the state with InterlockedCompareExchange16:
if (InterlockedCompareExchange16 (static_cast<state_type *>(&state), STATE::PRESENT, STATE::EMPTY) == STATE::EMPTY)
{
}
I am getting the following errors from VS2012:
My
static_assertfails complaining thatstate_typeis not the same size asshortThe
static_castcomplains that it cannot cast fromvolatile STATE *tostate_type *
Please can anyone give me any pointers for how to best fix my code?