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

后端 未结 6 484
别那么骄傲
别那么骄傲 2020-11-27 03:58

In kernel.h min is defined as:

#define min(x, y) ({                \\
    typeof(x) _min1 = (x);          \\
    typeof(y) _min2 = (y);          \\
    (void         


        
6条回答
  •  一向
    一向 (楼主)
    2020-11-27 04:46

    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.

提交回复
热议问题