Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

std::vector<bool>

162 views
Skip to first unread message

AP

unread,
Sep 18, 2014, 4:33:59 PM9/18/14
to
What is wrong with it ? I'm trying to digest Exceptional C++ on the subject
and can't get it ...


---
This email is free from viruses and malware because avast! Antivirus protection is active.
http://www.avast.com


--- news://freenews.netfront.net/ - complaints: ne...@netfront.net ---

Nobody

unread,
Sep 19, 2014, 12:20:54 AM9/19/14
to
On Thu, 18 Sep 2014 16:33:07 -0400, AP wrote:

> What is wrong with it ? I'm trying to digest Exceptional C++ on the
> subject and can't get it ...

std::vector<bool> has a relaxed specification relative to a vector of any
other type, so that the implementation can store each element as a single
bit.

Consequently, code which is valid for a vector of any other type (or even
for other containers) isn't necessarily valid for std::vector<bool>.

The main problem is that std::vector<bool>::reference won't be just
"bool&", because you can't have a reference to a bit. Instead, it will be
a proxy which emulates a reference by providing "operator bool" and
"operator=" methods.

Similarly, you can't obtain a pointer to a bit; "&v[i]" will return a
pointer to the reference proxy. In turn, there's no guarantee that
"i == j" implies "&v[i] == &v[j]" (v[i] and v[j] will probably be
different objects which reference the same bit).

Rick C. Hodgin

unread,
Sep 19, 2014, 4:08:58 AM9/19/14
to
The C++ standards group should have invented
a new "bit" or "bbool" (bbool) type for this
allowance.

Best regards,
Rick C. Hodgin

Rick C. Hodgin

unread,
Sep 19, 2014, 4:13:08 AM9/19/14
to
Oops... (bbool) should have been (bit bool).

Paavo Helde

unread,
Sep 19, 2014, 4:25:28 AM9/19/14
to
"Rick C. Hodgin" <rick.c...@gmail.com> wrote in news:e4a3f336-7b54-
4164-93f7-1...@googlegroups.com:

> The C++ standards group should have invented
> a new "bit" or "bbool" (bbool) type for this
> allowance.

Unfortunately, sizeof 1/8 is not representable in size_t.

Thus, the C++ standards group should have invented a special std::bitvector
instead.

Cheers
Paavo

Rick C. Hodgin

unread,
Sep 19, 2014, 4:46:28 AM9/19/14
to
On Friday, September 19, 2014 4:25:28 AM UTC-4, Paavo Helde wrote:
> "Rick C. Hodgin" <rick.c...@gmail.com> wrote:
> > The C++ standards group should have invented
> > a new "bit" or "bbool" (bbool) type for this
> > allowance.
>
> Unfortunately, sizeof 1/8 is not representable in size_t.
>
> Thus, the C++ standards group should have invented a special
> std::bitvector instead.

Yes. That would make it very clear.

David Harmon

unread,
Sep 20, 2014, 12:15:57 AM9/20/14
to
On Fri, 19 Sep 2014 03:25:13 -0500 in comp.lang.c++, Paavo Helde
<myfir...@osa.pri.ee> wrote,
>Thus, the C++ standards group should have invented a special std::bitvector
>instead.

Some of the purposes you have in mind may be served by std::bitset

Öö Tiib

unread,
Sep 21, 2014, 9:00:39 AM9/21/14
to
Yes. The controversial situation with 'vector<bool>' lost good opportunity
to make a clear distinction between bit-packed and not bit-packed
containers in standard library.

For bits there is 'boost::dynamic_bitset'; that is fine enough for
bools too.

On the other hand bit-packing may be sometimes beneficial with other
types as well. Typically there are integers or enums that have often
short range of meaningful values or some class types whose possible
states may be representable with only few bits. It seems more likely
that we have something like that in big numbers and not bools.

Mr Flibble

unread,
Sep 21, 2014, 2:38:49 PM9/21/14
to
On 21/09/2014 14:00, Öö Tiib wrote:
> On Friday, 19 September 2014 11:25:28 UTC+3, Paavo Helde wrote:
>> "Rick C. Hodgin" <rick.c...@gmail.com> wrote in news:e4a3f336-7b54-
>> 4164-93f7-1...@googlegroups.com:
>>
>>> The C++ standards group should have invented
>>> a new "bit" or "bbool" (bbool) type for this
>>> allowance.
>>
>> Unfortunately, sizeof 1/8 is not representable in size_t.
>>
>> Thus, the C++ standards group should have invented a special std::bitvector
>> instead.
>
> Yes. The controversial situation with 'vector<bool>' lost good opportunity
> to make a clear distinction between bit-packed and not bit-packed
> containers in standard library.

It is hard making sasuage fit into vector if sausage is uncooked; cooked
sausage has less problems.

AP

unread,
Oct 1, 2014, 4:57:09 PM10/1/14
to
> "Nobody" wrote in message
> news:pan.2014.09.19....@nowhere.invalid...
> std::vector<bool> has a relaxed specification relative to a vector of any
> other type, so that the implementation can store each element as a single
> bit.

vector<bool> bl={true, true, false, false, true, false};
auto head = bl.begin();
decltype(*head) BL;
for ( auto i : bl ) cout << i << endl;
BLref = *( bl.begin() );

g++ compiles without a problem - even the last line (which crashes at
runtime). CC chokes on the first line with
determine_type: type is 19
cg: assertion failed in file ../src/translator/sunIR.cc at line 294
cg: determine_type -- unexpected tword
cg: 1 errors
CC: cg failed for lam.cc
0 new messages