Does anyone know what code ":-!!" is in the kernel header?

Asked 2 years ago, Updated 2 years ago, 120 views

The code was imported from /usr/include/linux/kernel.h

/* Force a compilation error if condition is true, but also produce a
   result (of value 0 and type size_t), so the expression can be used
   e.g. in a structure initializer (or where-ever else comma expressions
   aren't permitted). */
#define BUILD_BUG_ON_ZERO(e) (sizeof(struct { int:-!!(e); }))
#define BUILD_BUG_ON_NULL(e) ((void *)sizeof(struct { int:-!!(e); }))

Here :-!!I don't know what is. Why did I use !! instead of ! to check true/false?

linux c linux-kernel macro

2022-09-21 14:54

1 Answers

This is a code that checks if e can be given a value of 0, and if it cannot be 0, it will fail during the build process.

Personally, the macro name is ...If you say BUILD_BUG_OR_ZERO rather than ON_ZERO, it would be a more accurate expression.

This code sizeof(structure { int: -!!(e);}) is read

It would have been better to use assert(), but the reason why I did this without assert() is because assert() can catch errors in runtime tests This macro can catch errors in compilation tests earlier than that.


2022-09-21 14:54

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.