> -----Original Message----- > From: owner-petsc-dev at mcs.anl.gov On Behalf Of Boyce Griffith > Sent: Wednesday, September 24, 2008 12:24 PM > To: petsc-dev at mcs.anl.gov > Subject: Re: PetscTruth bool? > > Hi, Barry et al. -- > > I may be mistaken, but I believe that sizeof(bool) is almost always one > bit in C++. This results in various problems, including the need to > have a specialized implementation of std::vector<bool> in the > STL.
If I am not mistaken, the reason std::vector is specialized for bool is to represent each boolean entry by 1 bit instead of 1 byte. The vector<bool> specialization is not needed because sizeof(bool) happens to be 1. For example, vector<char> is not specialized even if sizeof(char) is 1. vector<bool> was made different only to save 7 bits per entry by using only 1 bit to check for the boolean. Sutter mentions that "sizeof(bool) is implementation-defined, but it must be at least 1." From http://www.gotw.ca/gotw/050.htm . > See, e.g., > > http://www.informit.com/guides/content.aspx?g=cplusplus&seqNum=98 BTW, the code written in that article does not compile on the compilers of that era (MSVC 2003). I think gcc would have behaved the same way since it is usually stricter than the MS compiler. I am not sure what compiler the author used to claim that the code compiles and crashes, but the crash could be because the vector was empty and he called the front() method. Chetan
