[template.bitset] (original) (raw)

22.9.2.1 General [template.bitset.general]

namespace std { template<size_t N> class bitset { public: class reference { public: constexpr reference(const reference&) = default;constexpr ~reference();constexpr reference& operator=(bool x) noexcept; constexpr reference& operator=(const reference&) noexcept; constexpr bool operator~() const noexcept; constexpr operator bool() const noexcept; constexpr reference& flip() noexcept; };constexpr bitset() noexcept;constexpr bitset(unsigned long long val) noexcept;template<class charT, class traits, class Allocator> constexpr explicit bitset( const basic_string<charT, traits, Allocator>& str,typename basic_string<charT, traits, Allocator>::size_type pos = 0,typename basic_string<charT, traits, Allocator>::size_type n= basic_string<charT, traits, Allocator>::npos, charT zero = charT('0'), charT one = charT('1'));template<class charT, class traits> constexpr explicit bitset( basic_string_view<charT, traits> str,typename basic_string_view<charT, traits>::size_type pos = 0,typename basic_string_view<charT, traits>::size_type n= basic_string_view<charT, traits>::npos, charT zero = charT('0'), charT one = charT('1'));template<class charT> constexpr explicit bitset( const charT* str,typename basic_string_view<charT>::size_type n = basic_string_view<charT>::npos, charT zero = charT('0'), charT one = charT('1'));constexpr bitset& operator&=(const bitset& rhs) noexcept;constexpr bitset& operator|=(const bitset& rhs) noexcept;constexpr bitset& operator^=(const bitset& rhs) noexcept;constexpr bitset& operator<<=(size_t pos) noexcept;constexpr bitset& operator>>=(size_t pos) noexcept;constexpr bitset operator<<(size_t pos) const noexcept;constexpr bitset operator>>(size_t pos) const noexcept;constexpr bitset& set() noexcept;constexpr bitset& set(size_t pos, bool val = true);constexpr bitset& reset() noexcept;constexpr bitset& reset(size_t pos);constexpr bitset operator~() const noexcept;constexpr bitset& flip() noexcept;constexpr bitset& flip(size_t pos);constexpr bool operator[](size_t pos) const;constexpr reference operator[](size_t pos);constexpr unsigned long to_ulong() const;constexpr unsigned long long to_ullong() const;template<class charT = char,class traits = char_traits<charT>,class Allocator = allocator<charT>> constexpr basic_string<charT, traits, Allocator> to_string(charT zero = charT('0'), charT one = charT('1')) const;constexpr size_t count() const noexcept;constexpr size_t size() const noexcept;constexpr bool operator==(const bitset& rhs) const noexcept;constexpr bool test(size_t pos) const;constexpr bool all() const noexcept;constexpr bool any() const noexcept;constexpr bool none() const noexcept;};template<class T> struct hash;template<size_t N> struct hash<bitset<N>>;}

The class templatebitset<N>describes an object that can store a sequence consisting of a fixed number of bits, N.

Each bit represents either the value zero (reset) or one (set).

Totogglea bit is to change the value zero to one, or the value one to zero.

Each bit has a non-negative position pos.

When converting between an object of classbitset<N>and a value of some integral type, bit position pos corresponds to thebit value 1 << pos.

The integral value corresponding to two or more bits is the sum of their bit values.

The functions described in [template.bitset] can report three kinds of errors, each associated with a distinct exception:

22.9.2.2 Constructors [bitset.cons]

constexpr bitset() noexcept;

Effects: Initializes all bits in *this to zero.

constexpr bitset(unsigned long long val) noexcept;

Effects: Initializes the first M bit positions to the corresponding bit values in val.

M is the smaller of N and the number of bits in the value representation ([basic.types.general]) of unsigned long long.

If M < N, the remaining bit positions are initialized to zero.

template<class charT, class traits, class Allocator> constexpr explicit bitset( const basic_string<charT, traits, Allocator>& str,typename basic_string<charT, traits, Allocator>::size_type pos = 0,typename basic_string<charT, traits, Allocator>::size_type n= basic_string<charT, traits, Allocator>::npos, charT zero = charT('0'), charT one = charT('1'));template<class charT, class traits> constexpr explicit bitset( basic_string_view<charT, traits> str,typename basic_string_view<charT, traits>::size_type pos = 0,typename basic_string_view<charT, traits>::size_type n= basic_string_view<charT, traits>::npos, charT zero = charT('0'), charT one = charT('1'));

Effects: Determines the effective lengthrlen of the initializing string as the smaller ofn andstr.size() - pos.

Initializes the first M bit positions to values determined from the corresponding characters in the stringstr.

M is the smaller of N and rlen.

An element of the constructed object has value zero if the corresponding character in str, beginning at positionpos, iszero.

Otherwise, the element has the value one.

Character position pos + M - 1 corresponds to bit position zero.

Subsequent decreasing character positions correspond to increasing bit positions.

If M < N, remaining bit positions are initialized to zero.

The function uses traits​::​eqto compare the character values.

Throws: out_of_range if pos > str.size() orinvalid_argument if any of the rlen characters in strbeginning at position posis other than zero or one.

template<class charT> constexpr explicit bitset( const charT* str,typename basic_string_view<charT>::size_type n = basic_string_view<charT>::npos, charT zero = charT('0'), charT one = charT('1'));

Effects: As if by:bitset(n == basic_string_view<charT>::npos? basic_string_view<charT>(str) : basic_string_view<charT>(str, n),0, n, zero, one)

22.9.2.3 Members [bitset.members]

constexpr bitset& operator&=(const bitset& rhs) noexcept;

Effects: Clears each bit in*thisfor which the corresponding bit in rhs is clear, and leaves all other bits unchanged.

constexpr bitset& operator|=(const bitset& rhs) noexcept;

Effects: Sets each bit in*thisfor which the corresponding bit in rhs is set, and leaves all other bits unchanged.

constexpr bitset& operator^=(const bitset& rhs) noexcept;

Effects: Toggles each bit in*thisfor which the corresponding bit in rhs is set, and leaves all other bits unchanged.

constexpr bitset& operator<<=(size_t pos) noexcept;

Effects: Replaces each bit at position I in*thiswith a value determined as follows:

constexpr bitset& operator>>=(size_t pos) noexcept;

Effects: Replaces each bit at position I in*thiswith a value determined as follows:

constexpr bitset operator<<(size_t pos) const noexcept;

Returns: bitset(*this) <<= pos.

constexpr bitset operator>>(size_t pos) const noexcept;

Returns: bitset(*this) >>= pos.

constexpr bitset& set() noexcept;

Effects: Sets all bits in*this.

constexpr bitset& set(size_t pos, bool val = true);

Effects: Stores a new value in the bit at position pos in*this.

If val is true, the stored value is one, otherwise it is zero.

Throws: out_of_range if pos does not correspond to a valid bit position.

constexpr bitset& reset() noexcept;

Effects: Resets all bits in*this.

constexpr bitset& reset(size_t pos);

Effects: Resets the bit at position pos in*this.

Throws: out_of_range if pos does not correspond to a valid bit position.

constexpr bitset operator~() const noexcept;

Effects: Constructs an object x of classbitsetand initializes it with*this.

constexpr bitset& flip() noexcept;

Effects: Toggles all bits in*this.

constexpr bitset& flip(size_t pos);

Effects: Toggles the bit at position pos in*this.

Throws: out_of_range if pos does not correspond to a valid bit position.

constexpr bool operator[](size_t pos) const;

Hardened preconditions: pos < size() is true.

Returns: true if the bit at position pos in *this has the value one, otherwise false.

constexpr bitset::reference operator[](size_t pos);

Hardened preconditions: pos < size() is true.

Returns: An object of typebitset​::​referencesuch that(*this)[pos] == this->test(pos), and such that(*this)[pos] = valis equivalent tothis->set(pos, val).

Remarks: For the purpose of determining the presence of a data race ([intro.multithread]), any access or update through the resulting reference potentially accesses or modifies, respectively, the entire underlying bitset.

constexpr unsigned long to_ulong() const;

Throws: overflow_error if the integral value xcorresponding to the bits in *thiscannot be represented as type unsigned long.

constexpr unsigned long long to_ullong() const;

Throws: overflow_error if the integral value xcorresponding to the bits in *thiscannot be represented as type unsigned long long.

template<class charT = char,class traits = char_traits<charT>,class Allocator = allocator<charT>> constexpr basic_string<charT, traits, Allocator> to_string(charT zero = charT('0'), charT one = charT('1')) const;

Effects: Constructs a string object of the appropriate type and initializes it to a string of length N characters.

Each character is determined by the value of its corresponding bit position in*this.

Character position N - 1 corresponds to bit position zero.

Subsequent decreasing character positions correspond to increasing bit positions.

Bit value zero becomes the character zero, bit value one becomes the characterone.

Returns: The created object.

constexpr size_t count() const noexcept;

Returns: A count of the number of bits set in*this.

constexpr size_t size() const noexcept;

constexpr bool operator==(const bitset& rhs) const noexcept;

Returns: true if the value of each bit in*thisequals the value of the corresponding bit in rhs.

constexpr bool test(size_t pos) const;

Returns: trueif the bit at position posin*thishas the value one.

Throws: out_of_range if pos does not correspond to a valid bit position.

constexpr bool all() const noexcept;

Returns: count() == size().

constexpr bool any() const noexcept;

constexpr bool none() const noexcept;