Griwes
Griwes

Reputation: 9031

Is vector<bool> violating container requirements?

C++ standard draft n3242 in 23.2, in table containing container requirements, states that X::reference for container containing T must be lvalue T. Yet, for vector<bool>, vector<bool>::reference is another class, a proxy for accessing individual bits of bytes stored in vector.

Does that mean that std::vector class template specification for T = bool, defined in standard, fails to fulfill container requirements?

Upvotes: 4

Views: 275

Answers (4)

Galimov Albert
Galimov Albert

Reputation: 7357

Yes, it do. First, it uses proxy object vector <bool>::reference which is not actually a reference, but only looks same(its a class). Second, it have flip() method that other vectors does not have. Also, it does not support converting to C-like array unlike all other vectors: &vec[0].

So, actually vector< bool> is not a vector but looks like vector and its data is not a bool but look like bool. This container is worldwide considered as "standardized but failed thing".

Upvotes: 1

Pete Becker
Pete Becker

Reputation: 76245

Yes, vector<bool> does not meet the container requirements. It doesn't claim to, although there is a fairly clear implication. The thing is, the container "requirements" aren't requirements in any formal sense; there is nothing in the standard library that requires a type that meets the container requirements. Rather, the "requirements" are descriptive: each container's documentation can say (as does the documentation for vector<bool>) "this container meets the container requirements, except ...".

Upvotes: 4

Joel Falcou
Joel Falcou

Reputation: 6357

Yes, as stated here with a nice explanation.

Upvotes: 7

James McNellis
James McNellis

Reputation: 354979

Does that mean that std::vector class template specification for T = bool, defined in standard, fails to fulfill container requirements?

Yes.

Similarly, its iterators are not truly random access iterators, because operator* yields a proxy object.

vector<bool> is a mess.

Upvotes: 10

Related Questions