3

I was looking through linux kernel source code (kernel.h) and I found this macro for min function:

#ifndef max
#define max(x, y) ({                \
    typeof(x) _max1 = (x);          \
    typeof(y) _max2 = (y);          \
    (void) (&_max1 == &_max2);      \
    _max1 > _max2 ? _max1 : _max2; })
#endif

And now I'm wondering what does (void) (&_max1 == &_max2); line do?

PepeHands
  • 1,368
  • 5
  • 20
  • 36

1 Answers1

5

It prevents accidental type casting of x or y. You can arithmetical compare differently sized integers with the same sign, but you must not compare their pointers. I.e. this code would generate a compiler warning:

short a = 47;
long b = 11;
min(a, b);

C.f. Is comparing two void pointers to different objects defined in C++?

Community
  • 1
  • 1
Kijewski
  • 25,517
  • 12
  • 101
  • 143