|
|
On Tue, 25 Mar 2008 19:47:39 +0100, Nicolas Alvarez
<nic### [at] gmailisthebestcom> wrote:
>> On Tue, 25 Mar 2008 17:34:14 +0100, Nicolas Alvarez
>> <nic### [at] gmailisthebestcom> wrote:
>>> C++ STL vector template class has a specialization for 'bool' that
>>> uses 1 bit per element too. That is, a vector<bool> takes 1 bit per
>>> element.
>> Yes, but that specialisation is largely considered a mistake because
>> it breaks the interface associated with the generic std::vector.
>
> What exactly is broken in the interface?
Well, it does not satisfy the requirements of an STL container, making it
incompatible for use with some algorithms and other containers.
For a concrete example, consider this code:
// Code begins
template < typename T >
void func()
{
std::vector< T > v( 10 );
T& ref = v[0];
}
// Code ends
It works with any instantiation of the generic std::vector - the standard
guarantees it - but fails with std::vector<bool>.
Also, things like 'std::stack< bool, std::vector<bool> >' do not work
either.
As someone else once put it: std::vector<bool>, while listed under
"containers" in the standard, is not a container, nor does it contain
bools.
If you really need a compact set of booleans, use std::bitset or
boost::dynamic_bitset instead.
--
FE
Post a reply to this message
|
|