Pointers and integers, and also booleans, are integral types. The first three are all either pointers or integers, and since they are all non-zero, they convert to the boolean value true
. The fourth value of type double
converts to a zero integral value and hence false
.
Conversion of doubles that are not representable as integral values (like infinity and NaN) is undefined.
See 4.9 for details, and also 4.12 for "Boolean conversions":
A prvalue of arithmetic, unscoped enumeration, pointer, or pointer to member type can be converted to a
prvalue of type bool. A zero value, null pointer value, or null member pointer value is converted to false;
any other value is converted to true.
Your 0.0
is an arithmetic type of zero value.
Perhaps you may not be familiar with string literals in C++: ""
denotes the array char[1] { 0 }
, and this array (of one element) decays to a pointer to its first element, which is necessarily a non-null pointer. Similarly, "asdf" denotes an array char[5] { 'a', 's', 'd', 'f', 0 }
, and again this decays to a (non-null) pointer to its first element. The actual value of the characters is entirely immaterial.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…