Skip to content

Instantly share code, notes, and snippets.

@thoughtpolice
Created June 13, 2012 19:54
Show Gist options
  • Save thoughtpolice/2926080 to your computer and use it in GitHub Desktop.
Save thoughtpolice/2926080 to your computer and use it in GitHub Desktop.
MACROS FTW
/* Static assertions.
** GCC 4.6 and Clang 3.0 has support for _Static_assert under C1x
** so we use that here if it's available.
*/
#if (defined(COMPILER_GNUC) && \
(4 < __GNUC__ || (__GNUC__ == 4 && 6 <= __GNUC_MINOR__))) \
|| __has_extension(c_static_assert)
# define STATIC_ASSERT(cond,m) _Static_assert(cond, m)
#else
/* Not GCC 4.6 || Clang 3.0 */
# define STATIC_ASSERT_NAME2(name, line) name ## line
# define STATIC_ASSERT_NAME(line) STATIC_ASSERT_NAME2(jas_assert_, line)
/* The unused 'm' is a transition into C1x _Static_assert */
# ifdef __COUNTER__
# define STATIC_ASSERT(cond,m) \
extern void STATIC_ASSERT_NAME(__COUNTER__)(int STATIC_ASSERTION_FAILED[(cond)?1:-1])
# else
# define STATIC_ASSERT(cond,m) \
extern void STATIC_ASSERT_NAME(__LINE__)(int STATIC_ASSERTION_FAILED[(cond)?1:-1])
# endif /* __COUNTER__ */
#endif /* !(GCC 4.6 || clang 3.0) */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment