Skip to content
Tutorials Center
My learning

Topics

C++ Reference ยท Containers

C++ std::vector<bool>

Reference for std::vector<bool>: the bit-packed specialization, the proxy reference that breaks auto and references, and the alternatives to use instead.

std::vector<bool> is a specialization of std::vector that the standard allows (and, in practice, requires) to pack the elements into bits, one bit per bool. That saves memory, about eight times compared with std::vector<char>, but it means the container no longer behaves like the other vectors. It does not store bool objects, so it cannot give out a bool&, and data() does not exist.

Instead, operator[] returns a small proxy object (std::vector<bool>::reference) that reads and writes a single bit. The proxy works for most everyday code but breaks generic code that assumes T&. Most style guides call vector<bool> a historical mistake and suggest alternatives.

C++
std::vector<bool> flags(8, false);
flags[3] = true;          // the proxy sets bit 3

Try it

C++ Proxy references in practice

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

C++ What does not work, and alternatives

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

Syntax

C++
template <class Allocator>
class vector<bool, Allocator>;

class std::vector<bool>::reference {
public:
    operator bool() const noexcept;
    reference& operator=(bool) noexcept;
    reference& operator=(const reference&) noexcept;
    void flip() noexcept;
};

void flip() noexcept;                     // invert all bits
static void swap(reference a, reference b) noexcept;
Name Description
reference Proxy for one bit. Converts to bool; assigning to it writes the bit.
flip() (vector) Inverts every bit in the container.
flip() (reference) Inverts that one bit.
swap(a, b) Static function that swaps the values of two elements.

Returns: operator[], at, front and back return reference (const versions return bool).

Errors / undefined behavior: the same preconditions as vector<T> (operator[] out of range is undefined; at() throws std::out_of_range). A proxy that outlives its vector, or one invalidated by reallocation, is dangling.

Usage notes

  • No data(), no T&, and &v[0] is not a bool*. Code that passes a vector's buffer to C functions cannot use it.
  • auto deduces the proxy: auto x = v[0]; gives a reference that tracks the vector, not a bool copy. Write bool x = v[0]; or auto x = static_cast<bool>(v[0]);.
  • Thread safety: two threads writing different elements of an ordinary vector is safe, but writing different vector<bool> elements can race, because both touch the same word.
  • Iterators are not references-based: they are not true forward iterators in the strict sense, and algorithms that require bool& (such as std::ranges::sort) can fail to compile.
  • Compact: a million flags takes about 125 KB, versus 1 MB for vector<char>. That is the one reason to keep it.
  • Alternatives: std::vector<char> or std::vector<std::uint8_t> for speed and real references, std::bitset<N> when the size is a compile-time constant, boost::dynamic_bitset for a runtime-sized bit set, or a wrapper struct with a bool.
  • Templates: in generic code, std::vector<T> with T = bool instantiates this specialization; static-assert against it or special-case it if you need T&.
  • std::vector<bool> is still a valid container, so size(), push_back, resize, erase and iteration work as usual.