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

strk at refractions.net strk at refractions.net
Wed Apr 26 06:36:19 EDT 2006


Mateusz, the role of vector<bool> used there is to "tag" each
vertex in a CoordinateSequence telling wheter or not it is going
to be into the result. Size of the CoordinateSequence is not
known at compile-time, and we need a random access iterator 
to couple each vertex with this information. 
If we want to keep tracking JTS as close as possible I'd
use small integers instead of bools and still use a vector.
Does it make sense ?

--strk;


On Sat, Apr 22, 2006 at 09:53:09PM +0200, Mateusz Å?oskot wrote:
> 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