3 回答

TA貢獻1895條經驗 獲得超7個贊
class A {};A operator+(A const &, A const &);int main () { A a; a + a; // Not a problem (void)operator+(a,a); // Using function call notation - so add the cast.

TA貢獻1827條經驗 獲得超8個贊
static_cast<void>(fn());

TA貢獻1812條經驗 獲得超5個贊
鑄造到void用于抑制未使用變量和未保存的返回值或表達式的編譯器警告。
“標準”(2003)在第5.2.9/4節中說,
任何表達式都可以顯式轉換為“cvvoid”類型。表達式值是棄置.
所以你可以寫:
//suppressing unused variable warnings
static_cast<void>(unusedVar);
static_cast<const void>(unusedVar);
static_cast<volatile void>(unusedVar);
//suppressing return value warnings
static_cast<void>(fn());
static_cast<const void>(fn());
static_cast<volatile void>(fn());
//suppressing unsaved expressions
static_cast<void>(a + b * 10);
static_cast<const void>( x &&y || z);
static_cast<volatile void>( m | n + fn());
所有表格均有效。我通常把它縮短為:
//suppressing expressions
(void)(unusedVar);
(void)(fn());
(void)(x &&y || z);
也沒問題。
- 3 回答
- 0 關注
- 493 瀏覽
添加回答
舉報