是if(double)有效的C ++? [英] Is if(double) valid C++?
本文介绍了是if(double)有效的C ++?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我刚刚遇到这行代码:
if( lineDirection.length2() ){...}
其中 length2
返回 double
。它让我困惑我0.0等于0, NULL
和/或 false
。
where length2
returns a double
. It kind of puzzles me that 0.0 is equivalent to 0, NULL
, and/or false
.
这是C ++标准的这一部分还是未定义的行为?
Is this part of the C++ standard or is it undefined behaviour?
推荐答案
这是一个非常标准的行为(布尔转换)
It is a very much Standard Behavior (Boolean Conversion)
$ 4.12 / 1 - 算术右值,
枚举,指针或指向
成员类型的指针可以转换为bool类型的
值。零值,
空指针值或空成员
指针值被转换to false;
任何其他值都转换为true。
$4.12/1 - "An rvalue of arithmetic, enumeration, pointer, or pointer to member type can be converted to an rvalue 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."
这篇关于是if(double)有效的C ++?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文