【问题标题】:How to detect if a compiler supports static_assert?如何检测编译器是否支持 static_assert?
【发布时间】:2014-10-22 13:19:13
【问题描述】:

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

【问题讨论】:

  • 你可以检查__STDC_VERSION__ >= 201100 或类似的

标签: c macros feature-detection c11 static-assert


【解决方案1】:

在 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是如何知道_Static_assert是否被支持的呢?
  • @yshui,像assert.h这样的标准头文件是平台提供的,所以他们有内在的魔力知道什么是支持或不支持。这正是它们在这里的原因,因此您不必担心这些功能。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多