0

在 VS 10 中,我收到警告:

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

试图编译

int x ;
static_cast<bool>(x);

如何编写不会导致此警告的代码?

4

3 回答 3

7

这个怎么样:

x != 0
于 2013-01-09T16:52:18.550 回答
2
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
于 2013-01-09T18:03:15.863 回答
1

这是一个愚蠢的警告,可以忽略/禁用。据我所知,没有性能问题。

于 2013-03-07T17:01:23.830 回答