473,385 Members | 1,732 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

vector<FakeBool>

I have been reading about the problems associated with vector<bool>.
Unfortunately, the usual work-arounds aren't viable for me, but I have
never seen anyone mention using a class that behaves like a bool:

class FakeBool
{
public:
FakeBool() : b_(false) {}
FakeBool(bool b) : b_(b) {}
operator bool() { return b_; }
bool* operator&() { return &b_; }
private:
bool b_;
};

int main()
{
vector<FakeBool> v(5);
bool* p = &v[0];
}

I presume this solution is never mentioned because it is obviously
wrong,
but it seems to work well for me. Am I missing something?
Jul 22 '05 #1
4 5274
"Jeff Paciga" <je*********@gmail.com> wrote in message
news:81*************************@posting.google.co m...
I have been reading about the problems associated with vector<bool>.
Unfortunately, the usual work-arounds aren't viable for me, but I have
never seen anyone mention using a class that behaves like a bool:

class FakeBool
{
public:
FakeBool() : b_(false) {}
FakeBool(bool b) : b_(b) {}
operator bool() { return b_; }
bool* operator&() { return &b_; } You may need to have more member functions,
including an operator=(bool).
private:
bool b_;
};

int main()
{
vector<FakeBool> v(5);
bool* p = &v[0]; Note that trying to access subsequent items with something
like p[1] will lead to undefined behavior (UB) - a no-no
(even though it may work on your specific platform).
}

I presume this solution is never mentioned because it is obviously
wrong, but it seems to work well for me. It might work, but is not portable.
Am I missing something?

This is what I am asking myself...
Why are the "usual work-arounds" not viable for you?

To work around the vector<bool> specialization, I'd rather
go for something like std::vector<unsigned char>.
Regards,
Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form

Jul 22 '05 #2
> > vector<FakeBool> v(5);
bool* p = &v[0]; Note that trying to access subsequent items with something
like p[1] will lead to undefined behavior (UB) - a no-no
(even though it may work on your specific platform).


Is that because sizeof(FakeBool) may be greater than sizeof(bool)
because of byte alignment? I admit that I am quite confused about
how structs are stored internally, but I was hoping that since it was
just one bool that I was ok. I guess not.
Why are the "usual work-arounds" not viable for you?


Well, I want to be able to pass &v[0] to some crappy API that
needs an array of bools, so I can't use vector<char>, or
deque<bool>. I guess I will just have to do things the old-fashioned way.
Jul 22 '05 #3
"Jeff Paciga" <je*********@gmail.com> wrote in message
news:81**************************@posting.google.c om...
> vector<FakeBool> v(5);
> bool* p = &v[0];

Note that trying to access subsequent items with something
like p[1] will lead to undefined behavior (UB) - a no-no
(even though it may work on your specific platform).


Is that because sizeof(FakeBool) may be greater than sizeof(bool)
because of byte alignment? I admit that I am quite confused about
how structs are stored internally, but I was hoping that since it was
just one bool that I was ok. I guess not.


Unfortunately, there is no guarantee that sizeof(FakeBool)==sizeof(bool),
even though it is likely to be the case on many platforms.
Why are the "usual work-arounds" not viable for you?


Well, I want to be able to pass &v[0] to some crappy API that
needs an array of bools, so I can't use vector<char>, or
deque<bool>. I guess I will just have to do things the old-fashioned way.


Eventually, one option you could also consider is to use
an std::basic_string<bool> ... I haven't looked into it,
but you will then be able to use the data() or c_str()
member functions for *read-only* access to a contiguous
array of bools.

Other than that, I'm afraid the standard library does
not offer a solution here.
Kind regards,
Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form


Jul 22 '05 #4
> Eventually, one option you could also consider is to use
an std::basic_string<bool>


That is interesting. I will definitely look into it.
Thanks for your help.
Jul 22 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.