6

I did some tests with the operator &=. As the following examples shows, this works for single bool type as well as a vector<int> type, but not vector<bool>.

#include <vector>
int main (){

    bool a, b;
    a &= b; // ok
    std::vector<bool> c(1);
    c[0] &= b; // error
    c[0] = c[0] & b; // ok
    std::vector<int> d(1);
    d[0] &= b; // ok

    return 0;
}

Can anyone tell what is going on here?

(I'm using gcc 4.4.3)

Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
kunigami
  • 3,046
  • 4
  • 26
  • 42

2 Answers2

8

vector<bool> is not a container actually . :P

The article says

(If anyone else had written vector, 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 specialization requirement so that vector really is a vector of plain old bools. Besides, it's mostly redundant: std::bitset was designed for this kind of thing.) ....

The reason std::vector is nonconforming is that it pulls tricks under the covers in an attempt to optimize for space: Instead of storing a full char or int for every bool (taking up at least 8 times the space, on platforms with 8-bit chars), it packs the bools and stores them as individual bits (inside, say, chars) in its internal representation. One consequence of this is that it can't just return a normal bool& from its operator[] or its dereferenced iterators[2]; instead, it has to play games with a helper "proxy" class that is bool-like but is definitely not a bool. Unfortunately, that also means that access into a vector is slower, because we have to deal with proxies instead of direct pointers and references.

Prasoon Saurav
  • 91,295
  • 49
  • 239
  • 345
5

The reason for this is that vector<bool> is explicitly called out in the standard to take only one bit per item.

The result of this is that operator[] for vector<bool> returns a proxy object instead of a direct reference like every other contained type. The proxy object then does not support operator&= (again most likely per standard although I don't have a reference).

You can use deque<bool> if you want to use a byte per item and have the operator work as expected.

Mark B
  • 95,107
  • 10
  • 109
  • 188