class template specialization


template < class T, class Alloc = allocator<T> > class vector; // generic template
template <class Alloc> class vector<bool,Alloc>;               // bool specialization
Vector of bool
This is a specialized version of vector, which is used for elements of type bool and optimizes for space.

It behaves like the unspecialized version of vector, with the following changes:
  • The storage is not necessarily an array of bool values, but the library implementation may optimize storage so that each value is stored in a single bit.
  • Elements are not constructed using the allocator object, but their value is directly set on the proper bit in the internal storage.
  • Member function flip and a new signature for member swap.
  • A special member type, reference, a class that accesses individual bits in the container's internal storage with an interface that emulates a bool reference. Conversely, member type const_reference is a plain bool.
  • The pointer and iterator types used by the container are not necessarily neither pointers nor conforming iterators, although they shall simulate most of their expected behavior.

These changes provide a quirky interface to this specialization and favor memory optimization over processing (which may or may not suit your needs). In any case, it is not possible to instantiate the unspecialized template of vector for bool directly. Workarounds to avoid this range from using a different type (char, unsigned char) or container (like deque) to use wrapper types or further specialize for specific allocator types.

bitset is a class that provides a similar functionality for fixed-size arrays of bits.

Template parameters

Type of the allocator object used to define the storage allocation model. By default, allocator<bool> is used, which defines the simplest memory allocation model and is value-independent.
Aliased as member type vector<bool>::allocator_type.

Member types

member typedefinitionnotes
value_typeThe first template parameter (bool)
allocator_typeThe second template parameter (Alloc)defaults to: allocator<bool>
referenceA specific member class (see reference below)
pointera type that simulates pointer behaviorconvertible to const_pointer
const_pointera type that simulates pointer to const behavior
iteratora type that simulates random access iterator behaviorconvertible to const_iterator
const_iteratora type that simulates random access iterator to const behavior
difference_typea signed integral typeusually the same as ptrdiff_t
size_typean unsigned integral typeusually the same as size_t

Member classes

Member functions

The specialization has the same member functions as the unspecialized vector, except data, emplace, and emplace_back, that are not present in this specialization.

It adds the following:

Non-member class specializations

Data races

Simultaneous access to different elements is not guaranteed to be thread-safe (as storage bytes may be shared by multiple bits).