What is the function of "(void) (&_min1 == &_min2)" in the min macro in kernel.h?

The statement

(void) (&_min1 == &_min2);

is a guaranteed "no-op". So the only reason it's there is for its side effects.

But the statement has no side effects!

However: it forces the compiler to issue a diagnostic when the types of x and y are not compatible.
Note that testing with _min1 == _min2 would implicitly convert one of the values to the other type.

So, that is what it does. It validates, at compile time, that the types of x and y are compatible.


The code in include/linux/kernel.h refers to this as an "unnecessary" pointer comparison. This is in fact a strict type check, ensuring that the types of x and y are the same.

A type mismatch here will cause a compilation error or warning.

Tags:

C

Linux Kernel