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

后端 未结 6 481
别那么骄傲
别那么骄傲 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:30

    The Linux kernel is full of stuff like this (gratuitous gcc-specific hacks for the sake of "type safety" and other similar considerations), and I would consider it very bad practice and urge you not to follow it unless someone requires you to.

    pmg is right about the purpose of the hack, but any sane person would define min as ((x)<(y)?(x):(y)).

    Note that the kernel definition precludes many correct usages, e.g. where one argument is int and another is long. I suspect what they really wanted to preclude is signedness mismatches, where for example min(-1,1U) is 1. A better way to assert this would be to use a compile-time assertion for ((1?-1:(x))<0)==((1?-1:(y))<0). Note that this does not require any gcc-specific hacks.

提交回复
热议问题