[vector.bool]

# 23 Containers library [[containers]](./#containers)

## 23.3 Sequence containers [[sequences]](sequences#vector.bool)

### 23.3.14 Specialization of vector for bool [vector.bool]

#### [23.3.14.1](#pspc) Partial class template specialization vector<bool, Allocator> [[vector.bool.pspc]](vector.bool.pspc)

[1](#pspc-1)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/containers.tex#L10444)

To optimize space allocation, a partial specialization of vector forbool elements is provided:namespace std {template<class Allocator>class vector<bool, Allocator> {public:// typesusing value_type = bool; using allocator_type = Allocator; using pointer = *implementation-defined*; using const_pointer = *implementation-defined*; using const_reference = bool; using size_type = *implementation-defined*; // see [[container.requirements]](container.requirements "23.2 Requirements")using difference_type = *implementation-defined*; // see [[container.requirements]](container.requirements "23.2 Requirements")using iterator = *implementation-defined*; // see [[container.requirements]](container.requirements "23.2 Requirements")using const_iterator = *implementation-defined*; // see [[container.requirements]](container.requirements "23.2 Requirements")using reverse_iterator = std::reverse_iterator<iterator>; using const_reverse_iterator = std::reverse_iterator<const_iterator>; // bit referenceclass [reference](#lib:vector%3cbool%3e,reference "23.3.14.1 Partial class template specialization vector<bool, Allocator> [vector.bool.pspc]") {public:constexpr reference(const reference&) = default; constexpr ~reference(); constexpr operator bool() const noexcept; constexpr reference& operator=(bool x) noexcept; constexpr reference& operator=(const reference& x) noexcept; constexpr const reference& operator=(bool x) const noexcept; constexpr void flip() noexcept; // flips the bit}; // construct/copy/destroyconstexpr vector() noexcept(noexcept(Allocator())) : vector(Allocator()) { }constexpr explicit vector(const Allocator&) noexcept; constexpr explicit vector(size_type n, const Allocator& = Allocator()); constexpr vector(size_type n, const bool& value, const Allocator& = Allocator()); template<class InputIterator>constexpr vector(InputIterator first, InputIterator last, const Allocator& = Allocator()); template<[*container-compatible-range*](container.intro.reqmts#concept:container-compatible-range "23.2.2.1 Introduction [container.intro.reqmts]")<bool> R>constexpr vector(from_range_t, R&& rg, const Allocator& = Allocator()); constexpr vector(const vector& x); constexpr vector(vector&& x) noexcept; constexpr vector(const vector&, const type_identity_t<Allocator>&); constexpr vector(vector&&, const type_identity_t<Allocator>&); constexpr vector(initializer_list<bool>, const Allocator& = Allocator()); constexpr ~vector(); constexpr vector& operator=(const vector& x); constexpr vector& operator=(vector&& x)noexcept(allocator_traits<Allocator>::propagate_on_container_move_assignment::value || allocator_traits<Allocator>::is_always_equal::value); constexpr vector& operator=(initializer_list<bool>); template<class InputIterator>constexpr void assign(InputIterator first, InputIterator last); template<[*container-compatible-range*](container.intro.reqmts#concept:container-compatible-range "23.2.2.1 Introduction [container.intro.reqmts]")<bool> R>constexpr void assign_range(R&& rg); constexpr void assign(size_type n, const bool& t); constexpr void assign(initializer_list<bool>); constexpr allocator_type get_allocator() const noexcept; // iteratorsconstexpr iterator begin() noexcept; constexpr const_iterator begin() const noexcept; constexpr iterator end() noexcept; constexpr const_iterator end() const noexcept; constexpr reverse_iterator rbegin() noexcept; constexpr const_reverse_iterator rbegin() const noexcept; constexpr reverse_iterator rend() noexcept; constexpr const_reverse_iterator rend() const noexcept; constexpr const_iterator cbegin() const noexcept; constexpr const_iterator cend() const noexcept; constexpr const_reverse_iterator crbegin() const noexcept; constexpr const_reverse_iterator crend() const noexcept; // capacityconstexpr bool empty() const noexcept; constexpr size_type size() const noexcept; constexpr size_type max_size() const noexcept; constexpr size_type capacity() const noexcept; constexpr void resize(size_type sz, bool c = false); constexpr void reserve(size_type n); constexpr void shrink_to_fit(); // element accessconstexpr reference operator[](size_type n); constexpr const_reference operator[](size_type n) const; constexpr reference at(size_type n); constexpr const_reference at(size_type n) const; constexpr reference front(); constexpr const_reference front() const; constexpr reference back(); constexpr const_reference back() const; // modifierstemplate<class... Args> constexpr reference emplace_back(Args&&... args); constexpr void push_back(const bool& x); template<[*container-compatible-range*](container.intro.reqmts#concept:container-compatible-range "23.2.2.1 Introduction [container.intro.reqmts]")<bool> R>constexpr void append_range(R&& rg); constexpr void pop_back(); template<class... Args> constexpr iterator emplace(const_iterator position, Args&&... args); constexpr iterator insert(const_iterator position, const bool& x); constexpr iterator insert(const_iterator position, size_type n, const bool& x); template<class InputIterator>constexpr iterator insert(const_iterator position,
 InputIterator first, InputIterator last); template<[*container-compatible-range*](container.intro.reqmts#concept:container-compatible-range "23.2.2.1 Introduction [container.intro.reqmts]")<bool> R>constexpr iterator insert_range(const_iterator position, R&& rg); constexpr iterator insert(const_iterator position, initializer_list<bool> il); constexpr iterator erase(const_iterator position); constexpr iterator erase(const_iterator first, const_iterator last); constexpr void swap(vector&)noexcept(allocator_traits<Allocator>::propagate_on_container_swap::value || allocator_traits<Allocator>::is_always_equal::value); static constexpr void swap(reference x, reference y) noexcept; constexpr void flip() noexcept; // flips all bitsconstexpr void clear() noexcept; };}

[2](#pspc-2)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/containers.tex#L10568)

Unless described below, all operations have the same requirements and
semantics as the primary vector template, except that operations
dealing with the bool value type map to bit values in the
container storage and[allocator_traits​::​construct](allocator.traits.members#lib:allocator_traits,construct "20.2.9.3 Static member functions [allocator.traits.members]") is not used to construct these values[.](#pspc-2.sentence-1)

[3](#pspc-3)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/containers.tex#L10576)

There is no requirement that the data be stored as a contiguous allocation
of bool values[.](#pspc-3.sentence-1)

A space-optimized representation of bits is
recommended instead[.](#pspc-3.sentence-2)

[4](#pspc-4)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/containers.tex#L10581)

reference is a class that simulates the behavior of references of a single bit invector<bool>[.](#pspc-4.sentence-1)

The conversion function returns true when the bit is set, and false otherwise[.](#pspc-4.sentence-2)

The assignment operators
set the bit when the argument is (convertible to) true and
clear it otherwise[.](#pspc-4.sentence-3)

flip reverses the state of the bit[.](#pspc-4.sentence-4)

[ð](#lib:flip,vector%3cbool%3e)

`constexpr void flip() noexcept;
`

[5](#pspc-5)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/containers.tex#L10595)

*Effects*: Replaces each element in the container with its complement[.](#pspc-5.sentence-1)

[ð](#lib:swap,vector%3cbool%3e)

`static constexpr void swap(reference x, reference y) noexcept;
`

[6](#pspc-6)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/containers.tex#L10606)

*Effects*: Exchanges the contents of x and y as if by:bool b = x;
x = y;
y = b;

[ð](#pspc-itemdecl:3)

`template<class Allocator> struct hash<vector<bool, Allocator>>;
`

[7](#pspc-7)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/containers.tex#L10621)

The specialization is enabled ([[unord.hash]](unord.hash "22.10.19 Class template hash"))[.](#pspc-7.sentence-1)

[ð](#lib:is-vector-bool-reference)

`template<class T>
  constexpr bool is-vector-bool-reference = see below;
`

[8](#pspc-8)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/containers.tex#L10632)

The expression*is-vector-bool-reference*<T> is true if T denotes the type vector<bool, Alloc>​::​reference for some type Alloc andvector<bool, Alloc> is not a program-defined specialization[.](#pspc-8.sentence-1)

#### [23.3.14.2](#fmt) Formatter specialization for vector<bool> [[vector.bool.fmt]](vector.bool.fmt)

[ð](#lib:formatter)

namespace std {template<class T, class charT>requires *is-vector-bool-reference*<T>struct formatter<T, charT> {private: formatter<bool, charT> *underlying_*; // *exposition only*public:template<class ParseContext>constexpr typename ParseContext::iterator
 parse(ParseContext& ctx); template<class FormatContext>typename FormatContext::iterator
 format(const T& ref, FormatContext& ctx) const; };}

[ð](#lib:parse,formatter)

`template<class ParseContext>
  constexpr typename ParseContext::iterator
    parse(ParseContext& ctx);
`

[1](#fmt-1)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/containers.tex#L10671)

Equivalent to: return *underlying_*.parse(ctx);

[ð](#lib:format,formatter)

`template<class FormatContext>
  typename FormatContext::iterator
    format(const T& ref, FormatContext& ctx) const;
`

[2](#fmt-2)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/containers.tex#L10683)

Equivalent to: return *underlying_*.format(ref, ctx);
