简体   繁体   English

int转换为bool

[英]int to bool casting

In VS 10 I get a warning: 在VS 10中,我得到警告:

warning C4800: 'int' : forcing value to bool 'true' or 'false' (performance warning)

trying to compile 试图编译

int x ;
static_cast<bool>(x);

How is it possible to write a code which doesn`t cause this warning? 如何编写不会引起此警告的代码?

怎么样呢?

x != 0
int x ;
bool b1 = !!x;  // common idiom; get used to it.  "Newcomers" just need to learn the idiom.
bool b2 = x!=0; // another way, less idiomatic

It is a stupid warning and can be ignored/disabled. 这是一个愚蠢的警告,可以忽略/禁用。 There is no performance issue as far as I know. 据我所知,没有性能问题。

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

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