Skip to content

Commit d0ba2ab

Browse files
util: Add STATIC_ASSERT macro
1 parent d373bf6 commit d0ba2ab

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

src/util.h

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,27 @@ static void print_buf_plain(const unsigned char *buf, size_t len) {
5151
# define SECP256K1_INLINE inline
5252
# endif
5353

54+
/** Assert statically that expr is true.
55+
*
56+
* This is a statement-like macro and can only be used inside functions.
57+
*/
58+
#define STATIC_ASSERT(expr) do { \
59+
switch(0) { \
60+
case 0: \
61+
/* If expr evaluates to 0, we have two case labels "0", which is illegal. */ \
62+
case /* ERROR: static assertion failed */ (expr): \
63+
; \
64+
} \
65+
} while(0)
66+
5467
/** Assert statically that expr is an integer constant expression, and run stmt.
5568
*
5669
* Useful for example to enforce that magnitude arguments are constant.
5770
*/
5871
#define ASSERT_INT_CONST_AND_DO(expr, stmt) do { \
5972
switch(42) { \
60-
case /* ERROR: integer argument is not constant */ expr: \
73+
/* C allows only integer constant expressions as case labels. */ \
74+
case /* ERROR: integer argument is not constant */ (expr): \
6175
break; \
6276
default: ; \
6377
} \

0 commit comments

Comments
 (0)