Last active
August 27, 2022 15:29
-
-
Save LarryRuane/ce53d47588bd19f4c76e801b955ea729 to your computer and use it in GitHub Desktop.
example static assert implemention
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
#define static_assert(c) do { int _x = 1/((int)c); } while (0) | |
int main() { | |
static_assert(sizeof(int) == 4); | |
static_assert(sizeof(int) < 4); | |
return 0; | |
} | |
/* output: | |
$ gcc t.c | |
t.c: In function ‘main’: | |
t.c:1:41: warning: division by zero [-Wdiv-by-zero] | |
1 | #define static_assert(c) do { int _x = 1/((int)c); } while (0) | |
| ^ | |
t.c:5:5: note: in expansion of macro ‘static_assert’ | |
5 | static_assert(sizeof(int) < 4); | |
| ^~~~~~~~~~~~~ | |
$ | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment