Preface
The cause is this. Yesterday, when I was looking for C++11's range base for loop related stuff, I saw that vector<bool> is a proxy iterator, which is very special, so I curiously studied it.
First of all, vector<bool> is not a vector container in the usual sense, it originates from historical legacy issues.
As early as C++98, the type of vector<bool> was found, but because in order to consider the idea of saving space, the vector<bool> was not stored by one Byte, it was stored by one bit and one bit!
Because there is no direct bit to operate, when using operator[], the normal container should return a reference to the corresponding element, but for vector<bool>, it actually accesses a "proxy reference" instead of a "true reference", and returns an object of type "std::vector<bool>:reference".
Under normal circumstances
vector<bool> c{ false, true, false, true, false }; bool b = c[0]; auto d = c[0];
For the initialization of b, it actually implies an implicit type conversion. For d, its type is not bool, but an inner class in a vector<bool>.
If the value of d is modified at this time, the value in c will also be modified as well.
d = true; for(auto i:c) cout<<i<<" "; cout<<endl; //The above formula will output1 1 0 1 0
If c is destroyed, d will become a dangled pointer, and then the operation of d is an undefined behavior.
Therefore, it cannot satisfy some basic operations of the container, such as taking the address and pointer initialization operation [because there is no way to get the address for a single bit, or make references]
vector<bool> c{ false, true, false, true, false }; bool &tmp = c[0]; //Error, cannot be compiled, for reference, because c[0] is not an lvaluebool *p = &c[0]; //mistake,Cannot compile,Because a temporary address cannot be bound to a pointer
So why is it said that vector<bool> is not a standard container? It is because it cannot support the basic operations that some containers should have.
What is the correct way of using C++11's range-based for?
Clause 6: When auto deduces unexpected types, use explicit type initialization semantics
Summarize
The above is the entire content of this article. I hope that the content of this article has certain reference value for your study or work. Thank you for your support.