简体   繁体   English

将函数的整数返回值与C ++中的bool进行比较

[英]Comparing the integer return value of a function to bool in C++

Consider the following C++ code snippet below: 请考虑以下C ++代码段:

...

if (false == func()) // Line #1
{
 ...
 ...
}

int func()
{
 ...
  {
     ...
     return false;
  }
 ...

 return true;
} 

In the code snippet above, the prototype of function func() indicates that it returns an integer. 在上面的代码片段中,函数func()的原型表明它返回一个整数。 But inside the function func() , it returns either false or true . 但是在函数func() ,它返回falsetrue

Also in Line #1, the return value of function func() is checked against a boolean value. 同样在第1行中,函数func()的返回值是根据布尔值检查的。

I would like to know whether there is any problem with this type of usage. 我想知道这种用法是否有任何问题。 If there is some problem, recommend what rectifications needs to be done. 如果有问题,建议需要进行哪些整改。

There is no problem, any non-zero integer translates to true , and zero is false . 没有问题,任何非零整数都转换为true ,零为false Conversely, a bool can be converted to int , with true converting to 1 and false to 0 . 相反, bool可以转换为inttrue转换为1false转换为0

You don't need to be so verbose when comparing boolean values though: 比较布尔值时,您不需要如此冗长:

if (!func()) { .... }

Of course, if the function only returns true or false , as in your example, then it would make sense for it to return bool directly: 当然,如果函数只返回truefalse ,就像在你的例子中一样,那么它直接返回bool有意义的:

bool func();

From the standard 从标准

4.7 Integral conversions 4.7完整转换

If the destination type is bool, see 4.12. 如果目标类型是bool,请参见4.12。 If the source type is bool, the value false is converted to zero and the value true is converted to one. 如果源类型为bool,则将值false转换为零,将值true转换为一。

4.12 Boolean conversions [conv.bool] 4.12布尔转换[conv.bool]

A prvalue of arithmetic, unscoped enumeration, pointer, or pointer to member type can be converted to a prvalue of type bool. 算术,无范围枚举,指针或指向成员类型的指针的prvalue可以转换为bool类型的prvalue。 A zero value, null pointer value, or null member pointer value is converted to false; 零值,空指针值或空成员指针值转换为false; any other value is converted to true. 任何其他值都转换为true。 A prvalue of type std::nullptr_t can be converted to a prvalue of type bool; 类型为std :: nullptr_t的prvalue可以转换为bool类型的prvalue; the resulting value is false. 结果值为false。

Inside func , the bool you supply is converted to int for return. func里面,你提供的bool被转换为int以便返回。 false is converted to 0 and true is converted to 1 . false转换为0true转换为1

In the comparison, your constant false is converted to int , with value 0 . 在比较中,您的常量false将转换为int ,值为0

Generally your code will read better if you pick one: either use the bool type throughout, or else use the int type and the value 0 . 一般来说,如果你选择一个代码,你的代码会更好读:要么全部使用bool类型,要么使用int类型和值0

In the function func(), the source type is bool and the destination type is int (since the return type of the function is int), so the value false is converted to zero and the value true is converted to the value one of the destination type. 在函数func()中,源类型为bool,目标类型为int(因为函数的返回类型为int),因此值false将转换为零,值true将转换为值之一目的地类型。 This int, when returning back to Line #1 is automatically converted to bool so, since func can return 1 or 0, the value zero becomes false and 1 becomes true. 当返回到第1行时,这个int会自动转换为bool,因为func可以返回1或0,值0变为false,1变为true。 If func returned any non-zero value, it would be automatically converted to true on Line #1. 如果func返回任何非零值,它将在第1行自动转换为true。

Check this website in the sections "Integral conversions" and "Boolean conversions". 请在“积分转换”和“布尔转换”部分中查看此网站

Sureley there's no problem with this type of usage but I must say that it's not easily readable, expecially if the code becomes more complex and should be maintained by someone else that didn't write this code. Sureley对这种用法没有任何问题,但我必须说它不易读取,特别是如果代码变得更复杂并且应由未编写此代码的其他人维护。 Why don't you simply make func return bool? 你为什么不简单地让func返回bool?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM