|
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Member types | ||||
bitset::reference | ||||
| Member functions | ||||
(until C++20) | ||||
| Element access | ||||
| Capacity | ||||
| Modifiers | ||||
| Conversions | ||||
(C++11) | ||||
| Non-member functions | ||||
| Helper classes | ||||
(C++11) | ||||
class reference; | ||
Thestd::bitset class includesstd::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 ofstd::bitset::reference is to provide an lvalue that can be returned fromoperator[].
Any reads or writes to a bitset that happen via astd::bitset::reference potentially read or write to the entire underlying bitset.
Contents |
(constructor) | constructs the reference (public member function) |
(destructor) | destroys the reference (public member function) |
operator= | assigns a value to the referenced bit (public member function) |
operator bool | returns the referenced bit (public member function)[edit] |
operator~ | returns inverted referenced bit (public member function) |
flip | flips the referenced bit (public member function) |
reference(const reference&)=default; | (since C++11) (constexpr since C++23) | |
Constructs the reference from another reference. The copy constructor is implicitly declared.(until C++11)
Other constructors can only be accessed bystd::bitset.
~reference(); | (constexpr since C++23) | |
Destroys the reference.
reference& operator=(bool x); | (1) | (noexcept since C++11) (constexpr since C++23) |
reference& operator=(const reference& x); | (2) | (noexcept since C++11) (constexpr since C++23) |
Assigns a value to the referenced bit.
| x | - | value to assign |
*this
operatorbool()const; | (noexcept since C++11) (constexpr since C++23) | |
Returns the value of the referenced bit.
The referenced bit.
bool operator~()const; | (noexcept since C++11) (constexpr since C++23) | |
Returns the inverse of the referenced bit.
The inverse of the referenced bit.
reference& flip(); | (noexcept since C++11) (constexpr since C++23) | |
Inverts the referenced bit.
*this
#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: 12) bs: 1010; ref bit: 03) bs: 1110; ref bit: 14) bs: 1010; ref bit: 05) bs: 1110; ref bit: 16) ~ref bit: 0
| accesses specific bit (public member function)[edit] |