[geos-devel] Using std::vector<bool> container

Mateusz Łoskot mateusz at loskot.net
Sat Apr 22 15:53:09 EDT 2006


Hi,

Today, I noticed GEOS uses containers with elements of type of bool:
std::vector<bool>.

I reported those cases as a bug on the GEOS Bug Tracker with quite
detailed explanation, also giving references for further reading:

http://postgis.refractions.net/bugs/bug.php?op=show&bugid=101&pos=11

Simply, despite that using vector of bools is a bad idea from technical
point of view, it also does not make any sense.

What is following code suppose to do?

vector<bool> v;
bool* pv = &v[0];

Does it mean we retrieve address of single bit?
So, this code is compilable but only because of std::vector<bool> is e
pseudo-container, specialized by STL (C++ Language Standard, Chapter
23.2.5).
Complete explanation of this issue is available in Scott Meyers' book
(see report #101).

I think it's quite important to write about it here, just to warn
developers before using std::vector<bool> in future.

I refer interested developers to documents presented in bug report #101.

Instead of using std::vector<bool>, it's safe and recommended to use
deque<bool> or bitset. bitset is not a part of STL but is part of C++
Standard Library (CSL), so it's not an external dependency.

Cheers
-- 
Mateusz Łoskot
http://mateusz.loskot.net



More information about the geos-devel mailing list