std::vector::reference(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

πŸ’– NAME πŸ’–

okay bestie, this is std::vector::reference β€” page std::vector::reference(3) from the C++ Standard Libary manual. yes, that's the manual name, we're moving on πŸ’…


✨ Synopsis ✨

the code part is literally:

class reference;

so here's the tea, fr fr: the std::vector<bool, Alloc> specializations define std::vector<bool, Alloc>::reference as a publicly-accessible nested class. that 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[]. iconic.

⚠️ big vibe check: 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. not a drill, bestie 😭

🌸 Member functions 🌸

  • constructor β€” constructs the reference. Accessible only to std::vector<bool, Alloc> itself (public member function).
  • destructor β€” destroys the reference (public member function).
  • operator= β€” assigns a bool to the referenced bit (public member function).
  • operator bool β€” returns the referenced bit (public member function).
  • flip β€” flips the referenced bit (public member function).

πŸŽ€ std::vector<bool, Alloc>::reference::~reference πŸŽ€

the signatures:

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

Destroys the reference. that's it, she's done πŸ’€

πŸŽ€ std::vector<bool, Alloc>::reference::operator= πŸŽ€

the overloaded signatures, line wraps and all:

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

Assigns a value to the referenced bit. slay.

Parameters

  • x β€” value to assign

Return value

*this

πŸŽ€ std::vector<bool, Alloc>::reference::operator bool πŸŽ€

the signatures:

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

Returns the value of the referenced bit.

Parameters

(none)

Return value

The referenced bit.

πŸŽ€ std::vector<bool, Alloc>::reference::flip πŸŽ€

the signatures:

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

Inverts the referenced bit.

Parameters

(none)

Return value

(none)


🧚 Helper classes 🧚

πŸ¦‹ std::formatter<std::vector<bool, Alloc>::reference> πŸ¦‹

template < class T, class CharT >

  requires /* is-vector-bool-reference */<T>  (since C++23)

struct formatter<T, CharT>;

Specializes the std::formatter for std::vector<bool, Alloc>::reference. the specialization uses std::formatter<bool, CharT> as its underlying formatter (denoted as underlying_) where the referenced bit is converted to bool to be formatted.

the exposition-only constant /* is-vector-bool-reference */<T> is 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.

✨ Member functions ✨

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

πŸ“ Example πŸ“

This section is incomplete

Reason: no example


πŸ‘‘ See also πŸ‘‘

  • operator[] β€” access specified element (public member function of std::vector<T,Allocator>)
  • swap β€” swaps two std::vector<bool>::references [static] (public static member function)

"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).

πŸ¦‹ Category πŸ¦‹

  • * Todo no example

kisses from http://cppreference.com, version 2024.06.10, page std::vector::reference(3) β€” bye bestie, go read it again if you need to, mwah πŸ’‹βœ¨

πŸ“¦ where this page comes from