Enum variant values have to be constant expression integers. Division by 0 does not yield an integer so the compilation fails.
Created
February 19, 2023 20:47
-
-
Save shilangyu/7096faeeaa7cb84d980c05ef13a9eea9 to your computer and use it in GitHub Desktop.
Static assert in C
This file contains 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(cond, msg) enum { a = 1 / (cond) } | |
static_assert((SOME_CONST & (SOME_CONST - 1)) == 0, "SOME_CONST has to be a power of 2"); | |
int main() { | |
int arr[SOME_CONST]; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment