Namespaces
Variants

std:: vector<bool, Alloc>:: reference

From cppreference.net
class reference ;

std::vector < bool , Alloc > 的特化版本将 std::vector < bool , Alloc > :: reference 定义为一个可公开访问的嵌套类。 std::vector < bool , Alloc > :: reference 代理了指向 std::vector < bool , Alloc > 中单个比特位的引用行为。

std::vector < bool , Alloc > :: reference 的主要用途是提供一个可赋值的值,该值可从 operator [ ] 返回。

通过 std::vector < bool , Alloc > :: reference 对向量进行的任何读写操作,都可能读取或写入整个底层向量。

目录

成员函数

(constructor)
构造引用
(公开成员函数)
(destructor)
销毁引用
(公开成员函数)
operator=
为被引用的位赋值
(公开成员函数)
operator bool
返回被引用的位
(公开成员函数)
flip
翻转被引用的位
(公开成员函数)

std::vector<bool, Alloc>::reference:: reference

reference ( const reference & ) = default ;
(since C++11)
(constexpr since C++20)

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

其他构造函数只能通过 std::vector < bool , Alloc > 访问。

std::vector<bool, Alloc>::reference:: ~reference

~reference ( ) ;
(constexpr since C++20)

销毁该引用。

std::vector<bool, Alloc>::reference:: operator=

reference & operator = ( bool x ) ;
(1) (自 C++11 起为 noexcept)
(自 C++20 起为 constexpr)
constexpr const reference & operator = ( bool x ) const noexcept ;
(2) (自 C++23 起)
reference & operator = ( const reference & x ) ;
(3) (自 C++11 起为 noexcept)
(自 C++20 起为 constexpr)

为引用的位赋值。

const operator = 使得 std::vector<bool,Alloc>::iterator 能够满足 output_iterator 的要求。

(自 C++23 起)

参数

x - 要赋的值

返回值

* this

std::vector<bool, Alloc>::reference:: operator bool

operator bool ( ) const ;
(noexcept since C++11)
(constexpr since C++20)

返回被引用位的值。

返回值

被引用的位。

std::vector<bool, Alloc>::reference:: flip

void flip ( ) ;
(noexcept since C++11)
(constexpr since C++20)

反转所引用的位。

辅助类

std:: formatter <std::vector<bool, Alloc>::reference>

template < class T, class CharT >

requires /*is-vector-bool-reference*/ < T >

struct formatter < T, CharT > ;
(since C++23)

std::vector < bool , Alloc > :: reference 特化 std::formatter 。该特化使用 std:: formatter < bool , CharT > 作为其底层格式化器(记为 underlying_ ),其中被引用的位会被转换为 bool 类型进行格式化。

仅用于说明的常量 /*is-vector-bool-reference*/ < T > true 当且仅当 T 表示类型 std::vector < bool , Alloc > :: reference (对于某个类型 Alloc )且 std::vector < bool , Alloc > 不是 程序定义特化

成员函数

template < class ParseContext >
constexpr ParseContext :: iterator parse ( ParseContext & ctx ) ;
(1) (since C++23)
template < class FormatContext >
FormatContext :: iterator format ( const T & r, FormatContext & ctx ) const ;
(2) (since C++23)
1) 等价于 return underlying_ . parse ( ctx ) ;
2) 等价于 return underlying_ . format ( r, ctx ) ;

示例

参见

访问指定元素
( std::vector<T,Allocator> 的公开成员函数)
[static]
交换两个 std::vector<bool>:: reference
(公开静态成员函数)

外部链接

Scott Meyers所著《Effective Modern C++》(2015年)第2章第6条:"当 auto 推导出非预期类型时,使用显式类型初始化器惯用法"(第43-46页)——描述了代理类 std::vector<bool>::reference 可能存在的误用情况)。