20 Library introduction [library]

20.4 Method of description (Informative) [description]

20.4.2 Other conventions [conventions]

20.4.2.1 Type descriptions [type.descriptions]

20.4.2.1.4 Bitmask types [bitmask.types]

Several types defined in Clauses [language.support] through [thread] and Annex [depr] are bitmask types.
Each bitmask type can be implemented as an enumerated type that overloads certain operators, as an integer type, or as a bitset ([template.bitset]).
The bitmask type bitmask can be written:
// For exposition only.
// int_­type is an integral type capable of representing all values of the bitmask type.
enum bitmask : int_type {
  V = 1 << 0, V = 1 << 1, V = 1 << 2, V = 1 << 3, .....
};

inline constexpr bitmask C(V);
inline constexpr bitmask C(V);
inline constexpr bitmask C(V);
inline constexpr bitmask C(V);
  .....

constexpr bitmask operator&(bitmask X, bitmask Y) {
  return static_cast<bitmask>(
    static_cast<int_type>(X) & static_cast<int_type>(Y));
}
constexpr bitmask operator|(bitmask X, bitmask Y) {
  return static_cast<bitmask>(
    static_cast<int_type>(X) | static_cast<int_type>(Y));
}
constexpr bitmask operator^(bitmask X, bitmask Y){
  return static_cast<bitmask>(
    static_cast<int_type>(X) ^ static_cast<int_type>(Y));
}
constexpr bitmask operator~(bitmask X){
  return static_cast<bitmask>(~static_cast<int_type>(X));
}
bitmask& operator&=(bitmask& X, bitmask Y){
  X = X & Y; return X;
}
bitmask& operator|=(bitmask& X, bitmask Y) {
  X = X | Y; return X;
}
bitmask& operator^=(bitmask& X, bitmask Y) {
  X = X ^ Y; return X;
}
Here, the names C, C, etc.
 represent bitmask elements for this particular bitmask type.
All such elements have distinct, nonzero values such that, for any pair C and C where , & is nonzero and & is zero.
Additionally, the value 0 is used to represent an empty bitmask, in which no bitmask elements are set.
The following terms apply to objects and values of bitmask types:
  • To set a value Y in an object X is to evaluate the expression X |= Y.
  • To clear a value Y in an object X is to evaluate the expression X &= ~Y.
  • The value Y is set in the object X if the expression X & Y is nonzero.