|
|
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." "Data races Simultaneous access to different elements is not guaranteed to be thread-safe (as storage bytes may be shared by multiple bits). |
std::vector<bool> behaves similarly to std::vector, but in order to be space efficient, it: - Does not necessarily store its elements as a contiguous array (so &v[0] + n != &v[n]) - Exposes class std::vector<bool>::reference as a method of accessing individual bits. In particular, objects of this class are returned by operator[] by value. - Does not use std::allocator_traits::construct to construct bit values. |
Since its representation may by optimized, std::vector<bool> does not necessarily meet all Container or SequenceContainer requirements. For example, because std::vector<bool>::iterator is implementation-defined, it may not satisfy the ForwardIterator requirement. Use of algorithms such as std::search that require ForwardIterators may result in either compile-time or run-time errors. |
Well, in my case it was concurrent write, but each thread writing to their own exclusive section of the vector, at least so I thought. |
If you know the internal representation of std::vector<bool> you should know that. |
Item 18. Avoid using vector<bool>. As an STL container, there are really only two things wrong with vector<bool>. First, it's not an STL container. Second, it doesn't hold bools. Other than that, there's not much to object to. ... You may wonder why vector<bool> is in the Standard, given that it's not a container and all. The answer has to do with a noble experiment that failed, ... ... I mentioned earlier that proxy objects are often useful in C++ software development. The members of the C++ Standardization Committee were well aware of this, so they decided to develop vector<bool> as a demonstration of how the STL could support containers whose elements are accessed via proxies. With this example in the Standard, they reasoned, practitioners would have a ready reference for implementing their own proxy-based containers. Alas, what they discovered was that it was not possible to create proxy-based containers that satisfy all the requirements of STL containers. For one reason or another, however, their failed attempt at developing one remained in the Standard. One can speculate on why vector<bool> was retained, but practically speaking, it doesn't matter. What does matter is this: vector<bool> doesn't satisfy the requirements of an STL container; you're best off not using it; and deque<bool> and bitset are alternative data structures that will almost certainly satisfy your need for the capabilities promised by vector<bool>. - Scott Meyers in 'Effective STL' |
In 1998, admitting that the committee made a mistake was controversial. Since then Java has had to deprecate such significant portions of their libraries that the idea C++ would be ridiculed for deprecating a single minor template specialization seems quaint. ... vector<bool> is unusual, however, because it is a template specialization. Other than the flip() member function, most practical functionality is the same as for the primary template. Thus removing the specialization will not necessarily break user code, although it may alter the performance characteristics. I personally think that it would be preferable to remove the vector<bool> specialization rather than deprecate it, since the impact on users is so minimal, and since the sooner vector<bool> goes away, the better. Beman Dawes 'Fixing vector<bool>' http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2160.html |
(If anyone else had written vector<bool>, it would have been called "nonconforming" and "nonstandard." Well, it's in the standard, so that makes it a little bit harder to call it those names at this point, but some of us try anyway in the hopes that it will eventually get cleaned up. The correct solution is to remove the vector<bool> specialization requirement so that vector<bool> really is a vector of plain old bools. Besides, it's mostly redundant: std::bitset was designed for this kind of thing.) ... Bottom line: If you care more about speed than you do about size, you shouldn't use std::vector<bool>. Instead, you should hack around this optimization by using a std::vector<char> or the like instead, which is unfortunate but still the best you can do. - Herb Sutter http://www.gotw.ca/gotw/050.htm |
But it's implementation defined, so you can't assume to know what the internal representation is |
Different elements in the same container can be modified concurrently by different threads, except for the elements of std::vector<bool> (for example, a vector of std::future objects can be receiving values from multiple threads). |