Jump to content

  • Log In with Google      Sign In   
  • Create Account

Awesome job so far everyone! Please give us your feedback on how our article efforts are going. We still need more finished articles for our May contest theme: Remake the Classics

std::vector and bool


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
5 replies to this topic

#1 Evil Steve   Moderators   -  Reputation: 1918

Like
0Likes
Like

Posted 04 July 2006 - 01:24 AM

Hi all, I'm doing some bit-stuffing code at the moment, and I figured I could use std::vector<bool> to do that, since it's specialised (apparently). I know this is valid: std::vector<int> foo; // Stuff things into foo int* pData = &foo[0]; Since vector storage is contiguous. However, CodeWarrior spazzes out when I do this: std::vector<bool> foo; // Stuff a multiple of 8 bits into foo void* pData = &foo[0]; The error I get is:
Quote:
illegal explicit conversion from 'std::__bit_iterator<Metrowerks::__bitvec_deleter<std::allocator<bool>>, false>' to 'void *'
I presume that's because CodeWarrior isn't using a vector at all. Is what I'm trying to do valid (I.e. is it a compiler error), or are you not allowed to do this with a vector of bools? For the moment, I'm having to convert the vector into a void* manually, which isn't particularly efficient. Is there some std::copy I could use to do this which would be as efficient as direct pointer access? Cheers, Steve

Sponsor:

#2 rip-off   Moderators   -  Reputation: 5034

Like
0Likes
Like

Posted 04 July 2006 - 01:33 AM

The bool values in std::vectors<bool> are implemented using single bits. You cannot get a reference or pointer to a bit, so you get an error.

This is the downside to the apparent "optimisation" that is std:vector<bool>/

#3 Anonymous Poster_Anonymous Poster_*   Guests   -  Reputation:

0Likes

Posted 04 July 2006 - 01:35 AM

There are sometimes when standard namespaces templates just dont do it. I did something like this for a huffman encoding yet using unsigned char*(wrapped inside a class), using malloc instead of new (due to speed, ie remalloc instead of delete[] and then new again).

#4 Evil Steve   Moderators   -  Reputation: 1918

Like
0Likes
Like

Posted 04 July 2006 - 01:35 AM

Quote:
Original post by rip-off
The bool values in std::vectors<bool> are implemented using single bits. You cannot get a reference or pointer to a bit, so you get an error.

This is the downside to the apparent "optimisation" that is std:vector<bool>/
Ah well, that's what I thought. I'll just it my way then.

Thanks for the reply

#5 Will F   Members   -  Reputation: 1069

Like
0Likes
Like

Posted 04 July 2006 - 01:37 AM

What You Should Know about vector<bool> - it ain't pretty

#6 Evil Steve   Moderators   -  Reputation: 1918

Like
0Likes
Like

Posted 04 July 2006 - 01:57 AM

Quote:
Original post by Will F
What You Should Know about vector<bool> - it ain't pretty
I'm beginning to think that this was a bad idea [smile]
I'll re-write this later maybe.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS