Skip to content

Instantly share code, notes, and snippets.

@cpylua
Created August 12, 2011 01:48
Show Gist options
  • Select an option

  • Save cpylua/1141265 to your computer and use it in GitHub Desktop.

Select an option

Save cpylua/1141265 to your computer and use it in GitHub Desktop.
C Compile Time assert
#define COMPILE_TIME_ASSERT(expr) char UNIQUE_NAME[(expr) ? 1 : -1]
#define UNIQUE_NAME MAKE_NAME(__LINE__)
#define MAKE_NAME(line) MAKE_NAME2(line)
#define MAKE_NAME2(line) constraint_ ## line
COMPILE_TIME_ASSERT(sizeof(int) * 8 >= 64);
#ifndef CASSERT
#define CASSERT( exp, name ) typedef int dummy##name [ (exp ) ? 1 : -1 ];
#endif
CASSERT( sizeof(int) == 2, int_t_is_not_two_bytes )
#define MS_C_ASSERT(e) typedef char __MS_C_ASSERT__[(e)?1:-1]
MS_C_ASSERT(sizeof(int) < 4);
MS_C_ASSERT(sizeof(int) < 4); // redefinition
#define C_ASSERT(e) extern char __CASSERT__[(e)?1:-1]
C_ASSERT(sizeof(int) < 4);
C_ASSERT(sizeof(int) < 4);
C_ASSERT(sizeof(int) < 4);
C_ASSERT(sizeof(int) < 4);
C_ASSERT(sizeof(int) < 4);
C_ASSERT(sizeof(int) < 4);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment