Re: Scoped enum question
On 10.06.13 20.19, woodbrian77@gmail.com wrote:
uint8_t operator&(cmw_user_input_data_member_flags lhs,cmw_user_input_data_member_flags rhs)
{
return static_cast<uint8_t>(lhs)& static_cast<uint8_t>(rhs);
}
Since the opposite conversion from uint8_t to enum class is also not
implicit, you might prefer
cmw_user_input_data_member_flags
operator&(cmw_user_input_data_member_flags
lhs,cmw_user_input_data_member_flags rhs)
Because I need this type of operators quite often I have a macro for
this purpose.
#define FLAGSENUM(T) \
inline static T operator|(T l, T r) \
{ return (T)((unsigned)l|(unsigned)r); } \
inline static T operator&(T l, T r) \
{ return (T)((unsigned)l&(unsigned)r); } \
inline static T& operator|=(T& l, T r) \
{ return l = (T)((unsigned)l|(unsigned)r); } \
inline static T& operator&=(T& l, T r) \
{ return l = (T)((unsigned)l&(unsigned)r); } \
inline static T operator*(bool l, T r) \
{ return (T)(-l&(unsigned)r); } \
inline static T operator*(T l, bool r) \
{ return (T)((unsigned)l&-r); } \
inline static T operator~(T a) \
{ return (T)~(unsigned)a; }
Well, I know, macros are evil, but AFAIK there is definitely no way
around without repeating myself over and over.
Marcel