Skip to content

Instantly share code, notes, and snippets.

@sunny1304
Last active August 29, 2015 14:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sunny1304/af3e9e086961826bc09d to your computer and use it in GitHub Desktop.
Save sunny1304/af3e9e086961826bc09d to your computer and use it in GitHub Desktop.
Typecheck macro in C (from Linux Source)
//Use -Werror in GCC to get error, otherwise it will be a warning
/*
Constraints
One of the following shall hold:
both operands have arithmetic type;
both operands are pointers to qualified or unqualified versions of compatible types;
one operand is a pointer to an object or incomplete type and the other is a pointer to a qualified or unqualified version of void; or
one operand is a pointer and the other is a null pointer constant.
*/
#include <stdio.h>
#define typecheck(type, x) \
({ type _z; \
typeof(x) _y; \
(void)(&_y == &_z); \
1; \
})
int main(int argc, char* argv[])
{
printf("%d\n", typecheck(int, 10));
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment