命名空间
变体
操作

std::bitset<N>::reference

来自 cppreference.cn
< cpp‎ | 工具库‎ | bitset
 
 
 
 
class reference;

std::bitset 类包含 std::bitset::reference 作为可公开访问的嵌套类。此类的作用是代理对象,允许用户与 bitset 的单个位进行交互,因为标准 C++ 类型(如引用和指针)的精度不足以指定单个位。

std::bitset::reference 的主要用途是提供一个可以从 operator[] 返回的左值。

通过 std::bitset::reference 对 bitset 进行的任何读写操作都可能读写整个底层 bitset。

目录

[编辑] 成员函数

(构造函数)
构造引用
(公开成员函数)
(析构函数)
销毁引用
(公开成员函数)
operator=
为被引用位赋值
(公开成员函数)
operator bool
返回被引用位
(公共成员函数) [编辑]
operator~
返回被引用位的反转值
(公开成员函数)
flip
翻转被引用位
(公开成员函数)

std::bitset<N>::reference::reference

reference( const reference& ) = default;
(C++11 起)
(C++23 起为 constexpr)

从另一个引用构造引用。 复制构造函数被隐式声明。(C++11 前)

其他构造函数只能由 std::bitset 访问。

std::bitset<N>::reference::~reference

~reference();
(C++23 起为 constexpr)

销毁引用。

std::bitset<N>::reference::operator=

reference& operator=( bool x );
(1) (C++11 起无异常抛出)
(C++23 起为 constexpr)
reference& operator=( const reference& x );
(2) (C++11 起无异常抛出)
(C++23 起为 constexpr)

为被引用位赋值。

参数

x - 要赋的值

返回值

*this

std::bitset<N>::reference::operator bool

operator bool() const;
(C++11 起无异常抛出)
(C++23 起为 constexpr)

返回被引用位的值。

返回值

被引用位。

std::bitset<N>::reference::operator~

bool operator~() const;
(C++11 起无异常抛出)
(C++23 起为 constexpr)

返回被引用位的反转值。

返回值

被引用位的反转值。

std::bitset<N>::reference::flip

reference& flip();
(C++11 起无异常抛出)
(C++23 起为 constexpr)

反转被引用位。

返回值

*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';
}

输出

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

[编辑] 参阅

访问特定位
(公共成员函数) [编辑]