I have the following enum and input stream operator:
enum class MsrEncryptionStatus : unsigned char
{
Unknown = 0xFF,
None = 0,
Ksn,
Dukpt,
Other
};
inline std::istream& operator>>(std::istream& s, MsrEncryptionStatus& status)
{
return s >> static_cast<unsigned&>(status);
}
The above implementation of operator>> does not compile because of the way I'm doing the cast. Clang complains:
main.cpp:34:16: error: non-const lvalue reference to type 'unsigned int' cannot bind to a value of unrelated type 'MsrEncryptionStatus'
return s >> static_cast<unsigned&>(status);
^ ~~~~~~
What I'm trying to do is avoid defining a variable to temporarily hold the value from the input stream and then static_cast it later. Can someone help me understand what I'm missing here? I feel like I'm missing a fundamental piece of how static_cast functions on lvalues, for example it feels like a temporary is being created here and if that were true I can't bind it to a non-const lvalue reference.
template <typename E> inline typename std::enable_if<std::is_enum<E>::value, std::istream &>::type operator>>(std::istream &s, E &e) { typename std::underlying_type<E>::type tmp; s >> tmp; e = static_cast<E>(tmp); return s; }