【发布时间】:2014-10-22 13:19:13
【问题描述】:
我想在源文件中检测使用的编译器是否支持static_assert。
【问题讨论】:
-
你可以检查
__STDC_VERSION__ >= 201100或类似的
标签: c macros feature-detection c11 static-assert
我想在源文件中检测使用的编译器是否支持static_assert。
【问题讨论】:
__STDC_VERSION__ >= 201100 或类似的
标签: c macros feature-detection c11 static-assert
在 c11 中,static_assert 是一个扩展为 _Static_assert 的 assert.h 宏。
你可以使用:
#include <assert.h>
#if defined(static_assert)
// static_assert macro is defined
#endif
请注意,一些编译器(例如,IAR)也有一个 static_assert 关键字扩展,即使它们不支持 C11。
如 cmets 中所述,您还可以检查 c11:
#if (__STDC_VERSION >= 201112L)
// it is c11, static_assert is defined when assert.h is included
#endif
【讨论】:
assert.h这样的标准头文件是平台提供的,所以他们有内在的魔力知道什么是支持或不支持。这正是它们在这里的原因,因此您不必担心这些功能。