Last active
August 29, 2015 14:08
-
-
Save sunny1304/af3e9e086961826bc09d to your computer and use it in GitHub Desktop.
Typecheck macro in C (from Linux Source)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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