How does the following assert_disabled () macro work?

I see that this macro appears in many places in the code base to determine if a field is disabled or not (0 or 1).

#define assert_disabled(e) ((void)sizeof(e)) 

How does sizeof help find here if the field is 0 or 1?

Can someone explain this with a working example?

+4
source share
2 answers

I am sure this macro is just used when statements are disabled. The trick of using ((void)sizeof(e)) instead of just (void)0 or similar is smart: it avoids evaluating e (mostly), but still has a compiler check that e is a valid expression, so you won’t be surprised to compile errors when you change the definition to include statements.

+8
source

This is the black magic that I saw in the Linux kernel code.

Used to test the expression 'e' at compile time.

+2
source

Source: https://habr.com/ru/post/1441134/


All Articles