vector<FakeBool>

J

Jeff Paciga

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?
 
I

Ivan Vecerina

Jeff Paciga said:
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
 
J

Jeff Paciga

vector said:
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.
 
I

Ivan Vecerina

Jeff Paciga said:
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
 
J

Jeff Paciga

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,176
Messages
2,570,950
Members
47,503
Latest member
supremedee

Latest Threads

Top