David Abrahams wrote:
Nigel Rantor
writes:
Hi David, Well, I don't have access to the raw spec, I am working from "The C++ Language 3rd Ed." so if I make some errors due to this you'll have to forgive me.
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.
My understanding of uninitialised variables is that their *values* were undefined, that you could not rely on them to be any particular value, including not being within range for that type. So, you can read them, but there are no guarantees about what you'll get back. I suppose I agree, it really isn't a bool yet unless you can be assured that it contains true or false, but it does 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.
No, but I read it in the fourth para of sec 4.2 of TCPPPL. "In arithmetic and logical expressions, bools are converted to ints; integer arithmetic and logical operations are performed on the converted values."
, 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.
Computer or program? Really? Please elaborate, I'm interested. I don't see how accessing a piece of memory that hasn't been initialised to a legal value would cause that.
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.
I was simply using the OP's terminology, I apologise. s/1/true/ in my above sentence. :-) Anyway, the two approaches that I described for this problem still stand. Tell people to do it right or be lenient when parsing. If Robert can find a nice way of trapping uninitialised variables (not simply bools of course) and throw exceptions for these then that's great. I'm not sure that is entirely possible though. (and I haven't psent any time thinking about it either) n