A Specification for vector<bool>

Document Number: n2353(07-0213)
2007-07-19
Alisdair Meredith <public@alisdairm.net>

The Problem

The 2003 Standard requires a partial specialization of the standard library vector container for bool values. This specification has a number of problems, the most significant is that the existing specification leaves the semantics of all operations completely undefined. Given there is a widespread understanding that the intent was to mandate a space-optimized implementation and that flip operations would switch values from true to false and back again this paper provides wording for the missing semantics.

Proposed changes to the working paper

Add to clause 23.1.6 [vector.bool]:

-2- Unless described below, all operations have the same requirements and semantics as the primary vector template, except that operations dealing with the bool value type map to bit values in the container's storage.

-3- There is no requirement that the data is stored as a contiguous allocation of bool values. A space-optimized representation of bits is recommended instead.

-4- reference is a class that simulates the behavior of references of a single bit in vector<bool>. The conversion operator returns true when the bit is set, and false otherwise. The assignment operator sets the bit when the argument is (convertible to) true and clears it otherwise. flip reverses the state of the bit.

void flip()
-5- Replaces each element in the container with its complement. It is unspecified if it has any effect on allocated but unused bits.