c - 如何检测编译器是否支持 static_assert?

标签 c macros feature-detection c11 static-assert

我想在源文件中检测所使用的编译器是否支持 static_assert

最佳答案

在 c11 中,static_assert 是一个扩展为 _Static_assertassert.h 宏。

你可以只使用:

#include <assert.h>

#if defined(static_assert)
// static_assert macro is defined
#endif

请注意,某些编译器(例如 IAR)也有 static_assert 关键字扩展,即使它们不支持 C11。

如评论中所述,您还可以检查 c11:

#if (__STDC_VERSION >= 201112L)
// it is c11, static_assert is defined when assert.h is included
#endif

关于c - 如何检测编译器是否支持 static_assert?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25552852/

相关文章:

c - Variadic UNUSED 函数/宏

c - 如何使用 ## 运算符连接和计算宏

javascript - 如何使用特征检测来了解浏览器是否支持 css hover?

algorithm - 哪种特征检测器算法最容易学习?

c - 我是否需要为以下内容形成所有排列和组合?

c - 读取某些值并按 Enter 键时 fgets 发生冲突

c++ - 与 DEBUG 宏混淆

opencv - 如何在opencv中使用GenericIndex类

检查您是否位于主目录(linux)

c++ - typedef int* intptr 或 typedef int *intptr;