Namespaces
Variants

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

From cppreference.com
 
 
 
 
class reference;

The std::vector<bool, Alloc> specializations define std::vector<bool, Alloc>::reference as a publicly-accessible nested class. std::vector<bool, Alloc>::reference proxies the behavior of references to a single bit in std::vector<bool, Alloc>.

The primary use of std::vector<bool, Alloc>::reference is to provide an lvalue that can be returned from operator[].

Any reads or writes to a vector that happen via a std::vector<bool, Alloc>::reference potentially read or write to the entire underlying vector.

Member functions

(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)
returns the referenced bit
(public member function) [edit]
flip
flips the referenced bit
(public member function)

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

reference( const reference& other ) noexcept;
(since C++11)
(constexpr since C++20)

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::vector<bool, Alloc>.

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

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

Destroys the reference.

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

reference& operator=( bool other );
(1) (noexcept since C++11)
(constexpr since C++20)
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++20)

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::vector<bool, Alloc>::reference::operator bool

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

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::vector<bool, Alloc>::reference::flip

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

Inverts the referenced bit. Equivalent to *this = !*this.

Non-member functions

swap
swaps the (possibly referenced) boolean values
(function)

swap (std::vector<bool, Alloc>::reference)

friend void swap( reference x, reference y );
(1) (noexcept since C++11)
(constexpr since C++20)
friend void swap( reference x, bool& y );
(2) (noexcept since C++11)
(constexpr since C++20)
friend void swap( bool& x, reference y );
(3) (noexcept since C++11)
(constexpr since C++20)

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::vector<bool, Alloc>::reference is an associated class of the arguments.

Helper classes

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)

Specializes the std::formatter for std::vector<bool, Alloc>::reference.

Data members

Member Description
std::formatter<bool, CharT> underlying_ (private) the underlying formatter; the referenced bit is converted to bool to be formatted
(exposition-only member object*)

Member constants

Member Description
bool is-vector-bool-reference<T> [static] true if and only if T denotes the type std::vector<bool, Alloc>::reference for some type Alloc and std::vector<bool, Alloc> is not a program-defined specialization
(exposition-only static member constant*)

Member functions

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) Equivalent to return underlying_.parse(ctx);.
2) Equivalent to return underlying_.format(r, ctx);.

Example

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 3638
(P3612R1)
C++98 the swap function was a member of std::vector<bool> provided non-member versions

See also

access specified element
(public member function of std::vector<T,Allocator>) [edit]
[static] (deprecated)
swaps two std::vector<bool>::references
(public static member function) [edit]

External links

"Effective Modern C++" by Scott Meyers (2015), Chapter 2, Item 6: "Use the explicitly typed initializer idiom when auto deduces undesired types." (p.43-46) — describes a possible misuse of the proxy class std::vector<bool>::reference).