Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save nyrahul/242b705eb9fdbb2ab4349a3aa6685343 to your computer and use it in GitHub Desktop.
Save nyrahul/242b705eb9fdbb2ab4349a3aa6685343 to your computer and use it in GitHub Desktop.
// Example code which shows how to compare the two typedefs for size equivalency.
#define CONCAT_(a,b) a##b
#define CONCAT(a,b) CONCAT_(a, b)
#define STATIC_ASSERT(e) \
; enum { CONCAT(assert_line_, __LINE__) = 1/(int)(!!(e)) }
/* Note that ; is needed before enum if the STATIC_ASSERT is added right after case or label statement */
/* Note that multiple CONCATs are needed so that __LINE__ could be expanded .. Thus you can now have multiple STATIC_ASSERTs in the same code block */
typedef int uniqid_t;
typedef short mnid_t;
STATIC_ASSERT(sizeof(mnid_t) <= sizeof(uniqid_t));
int main(void)
{
int t = 1;
switch(t)
{
case 0:
STATIC_ASSERT(sizeof(mnid_t) <= sizeof(int));
break;
default:
break;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment