std::bitset::reference(3)
π std::bitset::reference β C++ Standard Library, section 3 (bestie edition)
okay so you're poking around in the C++ Standard Library, huh? This page is std::bitset::reference(3), and honestly? it's giving tiny-but-mighty. let's get into it π
β¨ NAME β¨
std::bitset::reference β literally std::bitset::reference π
πΈ SYNOPSIS πΈ
class reference;
so the std::bitset class includes std::bitset::reference as a publicly-accessible nested class. yes, nested. like a little bestie living inside the bitset π
what is it for? okay so listen β this class is used as a proxy object to let you interact with individual bits of a bitset. and why do we even need that? because standard C++ types (like references and pointers) are not built with enough precision to specify individual bits. like... they just can't point that fine. it's giving "trying to point at a single crumb in a whole cake" π
the primary use of std::bitset::reference is to provide an lvalue that can be returned from operator[]. iconic behavior honestly π
β οΈ heads up bestie: any reads or writes to a bitset that happen via a std::bitset::reference potentially read or write to the entire underlying bitset. so like... it's a proxy, not a laser. noted. π«Ά
π¦ Member functions π¦
- constructor β constructs the reference. Accessible only to
std::bitsetitself (private member function) π - destructor β destroys the reference (public member function)
- operator= β assigns a
boolto the referenced bit (public member function) - operator bool β 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(); (constexpr since C++23)
Destroys the reference. that's it. that's the whole vibe. bye bestie πβ¨
π std::bitset<N>::reference::operator=
reference& operator=( bool x ); (until C++11)
(since C++11)
reference& operator=( bool x ) noexcept; (constexpr since C++23)
reference& operator=( const reference& x ); (1) (until C++11)
reference& operator=( const reference& x ) noexcept; (2) (since C++11)
(constexpr since C++23)
Assigns a value to the referenced bit. slay, we love a good assignment π
π Parameters
xβ value to assign
π Return value
*this
π std::bitset<N>::reference::operator bool
operator bool() const; (until C++11)
operator bool() const noexcept; (since C++11)
(constexpr since C++23)
Returns the value of the referenced bit. the moment of truth honestly π
π Parameters
(none)
π Return value
The referenced bit.
π std::bitset<N>::reference::operator~
bool operator~() const; (until C++11)
bool operator~() const noexcept; (since C++11)
(constexpr since C++23)
Returns the inverse of the referenced bit. flipping it with style π¦
π Parameters
(none)
π Return value
The inverse of the referenced bit.
π std::bitset<N>::reference::flip
reference& flip(); (until C++11)
reference& flip() noexcept; (since C++11)
(constexpr since C++23)
Inverts the referenced bit. not a read, not a copy β an actual flip. periodt. π₯
π Parameters
(none)
π Return value
*this
π§ Example π§
// Run this code
#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
look at that little proxy doing its thing, we love to see it π₯Ήπ
π See also π
operator[]β accesses specific bit (public member function)
that's the whole page, bestie!! no cap, you're now a std::bitset::reference expert πβ¨
π this gem came fromhttp://cppreference.com, version2024.06.10, and the page itself isstd::bitset::reference(3). go forth and flip bits responsibly ππΈ
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms