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.

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 {
  \textit{V}0 = 1 << 0, \textit{V}1 = 1 << 1, \textit{V}2 = 1 << 2, \textit{V}3 = 1 << 3, .....
};

inline constexpr \textit{bitmask C}0(\textit{V}0);
inline constexpr \textit{bitmask C}1(\textit{V}1);
inline constexpr \textit{bitmask C}2(\textit{V}2);
inline constexpr \textit{bitmask C}3(\textit{V}3);
  .....

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 \textit{C}0, \textit{C}1, etc. represent bitmask elements for this particular bitmask type. All such elements have distinct, nonzero values such that, for any pair \textit{C}i and \textit{C}j where ij, Ci & Ci is nonzero and Ci & Cj 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: