C++ concepts: BitmaskType

< cpp‎ | concept
Revision as of 19:17, 12 November 2013 by (Talk)


Defines a type that can be used to represent a set of constant values or any combination of those values. This trait is typically implemented by integer types, std::bitset, or enumerations (scoped and unscoped) with additional operator overloads.


The bitmask type supports a finite number of bitmask elements, which are distinct values of the bitmask type, such that, for any pair Ci and Cj, Ci & Ci != 0 and Ci & Cj == 0.

The bitwise operators operator&, operator|, operator^, operator~, operator&=, operator|=, and operator^= are defined for values of the bitmask type and have the same semantics as the corresponding built-in operators on unsigned integers would have if the bitmask elements were the distinct integer powers of two.

The following expressions are well-formed and have the following meaning for any BitsetType X

X |= Y sets the value Y in the object X
X &= ~Y clears the value Y in the object X
(X&Y) != 0 indicates that the value Y is set in the object X

Each representible bitmask element is defined as a constexpr value of the bitmask type.


The following standard library types satisfy BitmaskType:

  • std::ctype_base::mask
  • std::ios_base::fmtflags
  • std::ios_base::iostate
  • std::ios_base::openmode
  • std::regex_traits::char_class_type
  • std::regex_constants::syntax_option_type
  • std::regex_constants::match_flag_type
  • std::launch,

Code that relies on some particular implementation option (e.g. int n = std::ios_base::hex), is nonportable because std::ios_base::fmtflags is not necessarily implicitly convertible to int.