Boost logo

Boost Users :

From: David Abrahams (dave_at_[hidden])
Date: 2005-11-10 11:24:31


Paul Giaccone <paulg_at_[hidden]> writes:

> Robert Ramey wrote:
>
>>Paul Giaccone wrote:
>>
>>
>>>For booleans, though, a value of other than 0 or 1 means it has not
>>>been initialised, and perhaps this should throw an exception on
>>>writing to the archive rather than on reading from it.
>>>
>>>
>>
>>Hmmm, I'm not sure about this. Do we know for a fact that a bool variable
>>will always contain 1 or 0? I've never seen code trap on an un-initialized
>>bool. It seems that even an uninitialized bool corresponds to true or
>>false.
>>
>>
> Is requiring the value to be 0 or 1 part of the C++ ANSI standard?

No, a bool has a value of true or false. 0 and 1 are integer values.

> If you want to try to reproduce the error, the code threw an exception
> on an object that was allocated on the heap and contained a boolean,
> which I had not initialised.

All bets are off then; you have no right to complain about anything
that happens afterwards. When an object containing an uninitialized
member is copied (as occurs when an exception is thrown), you get
undefined behavior.

>>Perhaps part of the problem is that I used 0 and 1 for bool variable
>>in order to not included english strings "true" and "false" in text files
>>and to save space.

       ar << (b ? 't' : 'f');

> This makes sense because 0 and 1 are probably what users would expect.
> Is it however possible to do the equivalent of boolalpha on the stream
> in order to write and read booleans as strings?

-- 
Dave Abrahams
Boost Consulting
www.boost-consulting.com

Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net