已引用此問題:Can an enum class be converted to the underlying type?。InterlockedCompareExchange16(帶有VS2012)的C++短枚舉問題
在我的代碼有有效:
enum class STATE : short
{
EMPTY,
PRESENT,
PARTIAL,
};
volatile STATE state;
然後我寫了typedef
和static_assert
:
typedef volatile std::underlying_type<STATE> state_type;
static_assert (sizeof (state_type) == sizeof (short), "Error - unsafe for use with InterlockedCompareExchange16");
最後,我試圖與InterlockedCompareExchange16
設置狀態:
if (InterlockedCompareExchange16 (static_cast<state_type *>(&state), STATE::PRESENT, STATE::EMPTY) == STATE::EMPTY)
{
}
我從VS2012中收到以下錯誤:
我
static_assert
失敗抱怨state_type
不是大小short
的
static_cast
同樣抱怨說,它不能從volatile STATE *
轉換爲state_type *
請任何人都可以給我任何指針如何最好地解決我的代碼?
請不要修改問題以回答問題。對於後來出現問題的人來說,這會讓人感到困惑,並使得答案變得多餘。回滾。 – hmjd
只是爲了承認我的失敗,之前提到的錯誤是錯誤地使用'underlying_type',但我仍然得到'static_cast'抱怨。 –
對不起@hmjd。我剛剛添加了解釋性評論 - 這足夠嗎? –