I want to detect, in source file, if the compiler used supports static_assert
.
Asked
Active
Viewed 2,696 times
8
1 Answers
15
In c11, static_assert
is an assert.h
macro that expands to _Static_assert
.
You can just use:
#include <assert.h>
#if defined(static_assert)
// static_assert macro is defined
#endif
Note that some compilers (e.g., IAR) also have a static_assert
keyword extension even if they don't support C11.
As mentioned in the comments you can also check for c11:
#if (__STDC_VERSION >= 201112L)
// it is c11, static_assert is defined when assert.h is included
#endif

ouah
- 142,963
- 15
- 272
- 331
-
1Then how does assert.h know if _Static_assert is supported? – nialv7 Aug 28 '14 at 16:31
-
6@yshui, the standard header files like `assert.h` are provided by the platform, so they have their internal magic to know what is supported or not. That is exactly whey they are here, so you don't have to worry about such features. – Jens Gustedt Aug 28 '14 at 18:50