2013-01-09 147 views
0

在VS 10,我得到一個警告:INT爲bool鑄造

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

試圖編譯

int x ; 
static_cast<bool>(x); 

怎麼可能寫出這不`噸導致此警告代碼?

+5

這不是一個錯誤,它是一個警告。 –

+1

關閉警告。 –

回答

1
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 
7

怎麼樣的東西是:

x != 0 
+0

其中有更清晰的優勢。 –

1

這是一個愚蠢的警告,可以忽略/禁用。據我所知,沒有性能問題。