Created
June 17, 2017 09:39
-
-
Save x100ex/f61d583b4d1c93868da24fa4d47bb262 to your computer and use it in GitHub Desktop.
static assert in C
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
// C compiler check for structure size | |
// from: http://bytes.com/topic/c/answers/220022-if-sizeof | |
#define _x_CCASERT_LINE_CAT(predicate, line) typedef char constraint_violated_on_line_##line[2*((predicate) != 0)-1]; | |
#define CCASSERT(predicate) _x_CCASERT_LINE_CAT(predicate, __LINE__) | |
// Usage: CCASSERT(1) to pass; CCASSERT(0) to fail | |
/* | |
typedef struct { | |
long x; | |
long y; | |
}foo ; | |
CCASSERT(sizeof(foo) < 10) // will not complain | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment