文章轉自 https://www.cnblogs.com/GyForever1004/p/8678554.html
如果有下面這段代碼:
#define min(x, y) ({ \
const typeof(x) _x = (x); \
const typeof(y) _y = (y); \
(void) (&_x == &_y); \
_x < _y ? _x : _y; })
int x = 2;
char y = 3;
int m;
m = min(x, y);
編譯的時候,經過預處理後,就會有這樣的判斷操作:
int * == char *;
因此編譯器就會提示:
warning: comparison of distinct pointer types lacks a cast
這個宏的巧妙之處就在於此。
所以,總結起來就是:
(void) (&_x == &_y) 用於判斷輸入的兩個值的類型是否是一致的。如果不一致,那麼編譯器就會做出如下警告:warning: comparison of distinct pointer types lacks a cast