2014-08-28 54 views

回答

10

在c11中,static_assert是一個assert.h宏,擴展爲_Static_assert

你可以使用:

#include <assert.h> 

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

注意,一些編譯器(例如,IAR)也有,即使他們不支持C11一static_assert關鍵字擴展。

正如在評論中提到,你還可以檢查C11:

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

那麼如何做ASSERT.H知道是否支持_Static_assert? – yshui 2014-08-28 16:31:37

+2

@yshui,像'assert.h'這樣的標準頭文件是由平臺提供的,所以他們有內在的魔力知道什麼是支持或不支持。這正是他們在這裏的乳清,所以你不必擔心這些功能。 – 2014-08-28 18:50:40