Skip to content

Instantly share code, notes, and snippets.

@yohhoy
Last active December 12, 2015 06:38
Show Gist options
  • Save yohhoy/4730420 to your computer and use it in GitHub Desktop.
Save yohhoy/4730420 to your computer and use it in GitHub Desktop.
snippet for Bitmask type(17.5.2.1.3 [bitmask.types])
#include <type_traits>
// 17.5.2.1.3 Bitmask types
enum class BitmaskType : unsigned int {
Field1 = 0x00000001,
Field2 = 0x00000002,
Field3 = 0x00000004,
Field4 = 0x00000008,
Field5 = 0x00000010,
//...
};
constexpr BitmaskType operator&(BitmaskType x, BitmaskType y)
{
typedef typename std::underlying_type<BitmaskType>::type UT;
return static_cast<BitmaskType>(static_cast<UT>(x) & static_cast<UT>(y));
}
constexpr BitmaskType operator|(BitmaskType x, BitmaskType y)
{
typedef typename std::underlying_type<BitmaskType>::type UT;
return static_cast<BitmaskType>(static_cast<UT>(x) | static_cast<UT>(y));
}
constexpr BitmaskType operator^(BitmaskType x, BitmaskType y)
{
typedef typename std::underlying_type<BitmaskType>::type UT;
return static_cast<BitmaskType>(static_cast<UT>(x) ^ static_cast<UT>(y));
}
constexpr BitmaskType operator~(BitmaskType x)
{
typedef typename std::underlying_type<BitmaskType>::type UT;
return static_cast<BitmaskType>(~static_cast<UT>(x));
}
BitmaskType & operator&=(BitmaskType & x, BitmaskType y)
{
x = x & y;
return x;
}
BitmaskType & operator|=(BitmaskType & x, BitmaskType y)
{
x = x | y;
return x;
}
BitmaskType & operator^=(BitmaskType & x, BitmaskType y)
{
x = x ^ y;
return x;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment