std::bitset<N>::reference
class reference;
|
||
The std::bitset class includes std::bitset::reference as a publicly-accessible nested class. This class is used as a proxy object to allow users to interact with individual bits of a bitset, since standard C++ types (like references and pointers) are not built with enough precision to specify individual bits.
The primary use of std::bitset::reference is to provide an lvalue that can be returned from operator[].
Any reads or writes to a bitset that happen via a std::bitset::reference potentially read or write to the entire underlying bitset.
Member functions
(constructor) |
constructs the reference (public member function) |
| destroys the reference (public member function) | |
operator= |
assigns a value to the referenced bit (public member function) |
| returns the referenced bit (public member function) | |
operator~ |
returns inverted referenced bit (public member function) |
flip |
flips the referenced bit (public member function) |
std::bitset<N>::reference::reference
reference( const reference& other ) noexcept;
|
(since C++11) (constexpr since C++23) |
|
Constructs the reference from other. The two references refer to the same bit.
|
The copy constructor is implicitly declared. |
(until C++11) |
Other constructors can only be accessed by std::bitset.
std::bitset<N>::reference::~reference
~reference();
|
(constexpr since C++23) | |
Destroys the reference.
std::bitset<N>::reference::operator=
reference& operator=( bool other );
|
(1) | (noexcept since C++11) (constexpr since C++23) |
constexpr const reference& operator=( bool other ) const noexcept;
|
(2) | (since C++23) |
reference& operator=( const reference& other );
|
(3) | (noexcept since C++11) (constexpr since C++23) |
Sets the bit referred to by *this when bool(other) is true, and clears it otherwise.
Parameters
| other | - | value to assign |
Return value
*this
std::bitset<N>::reference::operator bool
operator bool() const;
|
(noexcept since C++11) (constexpr since C++23) |
|
Returns the value of the referenced bit.
Return value
true if the value of the bit referred to by *this is one, false otherwise.
std::bitset<N>::reference::operator~
bool operator~() const;
|
(noexcept since C++11) (constexpr since C++23) |
|
Returns the inverse of the referenced bit.
Return value
true if the value of the bit referred to by *this is zero, false otherwise.
std::bitset<N>::reference::flip
reference& flip();
|
(noexcept since C++11) (constexpr since C++23) |
|
Inverts the referenced bit. Equivalent to *this = !*this.
Return value
*this
Non-member functions
swap |
swaps the (possibly referenced) boolean values (function) |
swap (std::bitset<N>::reference)
friend void swap( reference x, reference y );
|
(1) | (noexcept since C++11) (constexpr since C++23) |
friend void swap( reference x, bool& y );
|
(2) | (noexcept since C++11) (constexpr since C++23) |
friend void swap( bool& x, reference y );
|
(3) | (noexcept since C++11) (constexpr since C++23) |
Exchanges the values denoted by x and y as if by
bool b = x;
x = y;
y = b;
.
These functions are not visible to ordinary unqualified or qualified lookup, and can only be found by argument-dependent lookup when std::bitset<N>::reference is an associated class of the arguments.
Example
#include <bitset>
#include <iostream>
int main()
{
std::bitset<4> bs{0b1110};
std::bitset<4>::reference ref = bs[2];
auto info = [&](int id)
{
std::cout << id << ") bs: " << bs << "; ref bit: " << ref << '\n';
};
info(1);
ref = false;
info(2);
ref = true;
info(3);
ref.flip();
info(4);
ref = bs[1]; // operator=( const reference& x )
info(5);
std::cout << "6) ~ref bit: " << ~ref << '\n';
}
Output:
1) bs: 1110; ref bit: 1
2) bs: 1010; ref bit: 0
3) bs: 1110; ref bit: 1
4) bs: 1010; ref bit: 0
5) bs: 1110; ref bit: 1
6) ~ref bit: 0
Defect reports
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
| DR | Applied to | Behavior as published | Correct behavior |
|---|---|---|---|
| LWG 4187 (P3612R1) |
C++98 C++23 |
1. there was no swap function for std::bitset<N>::reference (C++98)2. std::bitset<N>::reference did not support const-assignment (C++23)
|
1. provided 2. supports |
See also
| accesses specific bit (public member function) |