?
本文檔使用 php中文網(wǎng)手冊 發(fā)布
_Static_assert ( expression , message ) | (since C11) |
---|
expression | - | any integer constant expression |
---|---|---|
message | - | any string literal |
此關(guān)鍵字也可用作方便宏static_assert
,可在標(biāo)題中找到<assert.h>
。
常量表達(dá)式在編譯時(shí)進(jìn)行評估并與零進(jìn)行比較。如果它比較等于零,則會(huì)發(fā)生編譯時(shí)錯(cuò)誤,編譯器必須顯示消息作為錯(cuò)誤消息的一部分(不需要顯示不在基本源字符集中的字符)。
否則,如果表達(dá)式不等于零,則什么都不會(huì)發(fā)生; 沒有代碼被發(fā)射。
_Static_assert
.
#include <assert.h>int main(void){ // Test if math works. static_assert(2 + 2 == 4, "Whoa dude!"); // or _Static_assert(... // This will produce an error at compile time. static_assert(sizeof(int) < sizeof(char), "this program requires that int is less than char");}