Boost logo

Boost Users :

From: David Abrahams (dave_at_[hidden])
Date: 2005-11-10 11:18:27


Nigel Rantor <wiggly_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.
>
> No, bools won't always contain 1 or 0, like other types their value is
> be undefined if they have not been initialised, depending upon where
> they have been declared.

An uninitialized bool doesn't have a value you can read without
causing undefined behavior. So for all intents and purposes it
doesn't have a value.

> <caveat>
> C++ isn't my day job...I just use it for fun things...
> </caveat>
>
> When bools are used in logical operations they are converted to
> integers

Can you cite the standard on that one? I'm pretty sure that it's the
other way around: when integers are used in logical operations they
are converted to bools.

> , so depending on what your bool happens to contain before
> initialisation it could evaluate to either true or false.

Or it could crash your computer.

>> 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.
>>
>> I'll think about this.
>
> My two proposals would be the one that encourages pragmatism and the
> other that encourages correctness.
>
> Pragmatic:
>
> Well, how about simply treating anything other than 1 as false?

I don't know what 1 has to do with anything. The values of a bool
are true and false. 1 is an int.

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