_STATIC_ASSERT 宏
在编译时计算表达式当如果结果为 FALSE,则会生成错误。
_STATIC_ASSERT(
booleanExpression
);
参数
- booleanExpression
计算表达式 (包含指针)为非0 (TRUE) 或 0 (FALSE)。
备注
此宏类似于 _ASSERT 和_ASSERTE 宏,但 booleanExpression 在编译时计算而非运行时。 如果 booleanExpression 计算结果为 FALSE (0),则生成 编译器错误 C2466。
示例
在此示例中,我们检查是否 sizeof int 大于或等于 2 字节,并且是否 sizeof long 为 1 字节。 程序将不编译,并将生成 编译器错误 C2466,由于 long 大于 1 字节。
// crt__static_assert.c
#include <crtdbg.h>
#include <stdio.h>
_STATIC_ASSERT(sizeof(int) >= 2);
_STATIC_ASSERT(sizeof(long) == 1); // C2466
int main()
{
printf("I am sure that sizeof(int) will be >= 2: %d\n",
sizeof(int));
printf("I am not so sure that sizeof(long) == 1: %d\n",
sizeof(long));
}
要求
宏 |
必需的标头 |
---|---|
_STATIC_ASSERT |
<CRTDBG.H> |
.NET Framework 等效项
System::Diagnostics::Debug::Assert