|
Boost Users : |
From: David Abrahams (dave_at_[hidden])
Date: 2005-11-10 13:54:34
Nigel Rantor <wiggly_at_[hidden]> writes:
> 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.
No.
4.1 Lvalue-to-rvalue conversion
1 An lvalue (3.10) of a non-function, non-array type T can be
converted to an rvalue. If T is an incomplete type, a program that
necessitates this conversion is ill-formed. If the object to which
the lvalue refers is not an object of type T and is not an object of
a type derived from T, or if the object is uninitialized, a program
that necessitates this conversion has undefined behavior.
5 Expressions [expr]
8 Whenever an lvalue expression appears as an operand of an operator
that expects an rvalue for that operand, the lvalue-to-rvalue (4.1),
array-to-pointer (4.2), or function-to-pointer (4.3) standard
conversions are applied to convert the expression to an
rvalue.
I don't have time to run down everything relevant right now, but
lvalue-to-rvalue conversion happens at the drop of a hat. I'm pretty
sure if you trace through it, you'll find out that anything you can do
that reads the value of an uninitialized variable will go through an
lvalue-to-rvalue conversion.
Not to mention Footnote 42, which though non-normative speaks
directly to this question:
42) Using a bool value in ways described by this International
Standard as ``undefined,'' such as by examining the value of an
uninitialized automatic variable, might cause it to behave as
if is neither true nor false.
>>> <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.
You should get a copy of the standard.
> "In arithmetic and logical expressions, bools are converted to ints;
> integer arithmetic and logical operations are performed on the converted
> values."
5.14 Logical AND operator 5 Expressions
1 The && operator groups left-to-right. The operands are both
implicitly converted to type bool (clause 4)...
>>> , 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?
Yes.
> Really? Please elaborate, I'm interested.
Undefined behavior means anything can happen. Really.
1.3.12 undefined behavior
behavior, such as might arise upon use of an erroneous program
construct or erroneous data, for which this International Standard
imposes no requirements.
^^^^^^^^^^^^^^^
> I don't see how accessing a piece of memory that hasn't been
> initialised to a legal value would cause that.
It depends on how the platform works. That's allowed under the C++
standard.
>>> 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. :-)
Since the two valid values of a bool are true and false, treating
anything other than true as false is tautological.
-- 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