Skip to content

Commit 0bcb32e

Browse files
committed
perl.h: Add comments about how STATIC_ASSERT works
I found these useful in understanding it
1 parent cf05d9a commit 0bcb32e

File tree

1 file changed

+27
-3
lines changed

1 file changed

+27
-3
lines changed

perl.h

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4282,9 +4282,33 @@ function or at file scope (outside of any function).
42824282
*/
42834283
# define STATIC_ASSERT_DECL(COND) static_assert(COND, #COND)
42844284
#else
4285-
/* We use a bit-field instead of an array because gcc accepts
4286-
'typedef char x[n]' where n is not a compile-time constant.
4287-
We want to enforce constantness.
4285+
4286+
/* This generates a struct with a bit field like so:
4287+
*
4288+
* struct { unsigned int name#: size; } name#
4289+
*
4290+
* 'name#' is the name followed by the line number this is used on. The
4291+
* name is 'static_assertion_failed_', so that a typical name would be
4292+
*
4293+
* static_assertion_failed_123
4294+
*
4295+
* The first name# will show up in the compiler's error message, clueing in the
4296+
* reader as to the problem and where. The second one makes sure that the
4297+
* struct name is unique to the compilation unit.
4298+
*
4299+
* 'size' is expressed as a ternary: '((cond) ? 1 : -1)'
4300+
*
4301+
* If 'cond' is true the size is 1, which is legal; if false, the size is -1.
4302+
* It is illegal to have a negatively-sized bit field, so the compilation will
4303+
* abort iff 'cond' is false, giving an appropriate error message.
4304+
*
4305+
* The basic struct is used in different ways depending on whether the
4306+
* application is for a declaration (typedef struct), or statement
4307+
* (STMT_START { struct } STMT_END).
4308+
*
4309+
* We use a bit-field instead of an array because gcc accepts
4310+
typedef char x[n]
4311+
where n is not a compile-time constant. We want to enforce constantness.
42884312
*/
42894313
# define STATIC_ASSERT_2(COND, SUFFIX) \
42904314
typedef struct { \

0 commit comments

Comments
 (0)