奇艺广州网站建设 熊掌号,怎么在自己的电脑做网站,苗族网站建设,公司网站上线的通知C中包含动态断言#xff08;assert#xff09;和静态断言#xff08;static_assert#xff09;#xff0c;下面分别分析各自的用法。
1.动态断言#xff08;assert#xff09; assert 是一个宏#xff0c;在预处理阶段不生效#xff0c;在运行阶段才起作用#xff0… C中包含动态断言assert和静态断言static_assert下面分别分析各自的用法。
1.动态断言assert assert 是一个宏在预处理阶段不生效在运行阶段才起作用所以又叫“动态断言”。 动态断言用来判定一个表达式必定为真。表达式如果返回false就会输出错误消息然后调用 abort() 终止程序的执行。
assert(i 0);
assert(p ! nullptr);
assert(!str.empty()); 上述断言分别在运行阶段判定1)变量i是整数2指针p不为空3字符串str不是空字符。 动态断言可以附加错误信息方便用户查看。
assert(i 0 i must be greater than zero);
assert(p ! nullptr p must not be null);
assert(!str.empty() str must not be empty);
2.静态断言static_assert static_assert是一个关键字而不是宏定义。它在编译阶段生效在运行阶段是看不到的所以又叫”静态断言“。 静态断言用来判定一个表达式必定为真。表达式如果返回false就会编译失败抛出错误信息。
static_assert(__GNUC__ || __clang__);
static_assert(_MSC_VER); 上述断言分别在编译阶段判定1)是否使用了 GCC 或 Clang 编译器2检查是否使用了 Microsoft Visual Studio 编译器 静态断言可以附加错误信息方便用户查看。
static_assert(std::is_sameint, int::value, C11 is not supported);
static_assert(std::is_null_pointerstd::nullptr_t::value, C14 is not supported); 上述断言分别在编译阶段判定1)是否支持 C11 的 std::is_same 类型特性2是否支持 C14 的 std::is_null_pointer 类型特性。