c - Desired effect of value-discarded comparision of GCC anonymous function variables' addresses -


the linux kernel defines number of helper macros type neutral numeric operations. namely macros min , max defined (in include/linux/kernel.h)

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

when expanded produce anonymous functions perform operation type neutral , in-expression-assigment safe way (using gcc anonymous functions). far good. surprised me second last expression produced these macros:

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

the addresses of temporary variables defined within scope of anonymous function compared , result discarded; (void) cast preventing warnings show up.

i wonder desired side effect of is? naively i'd expect optimized away, since has no side effects (at least none aware of).

the compiler complain == if both variables don't have same type. suspect ensure values can compared.

try:

int i; char *p; (void)min(i, p); 

and yes, operation optimised away optimisation level other -o0.


Comments

Popular posts from this blog

java - pagination of xlsx file to XSSFworkbook using apache POI -

Unlimited choices in BASH case statement -

apache - How do I stop my index.php being run twice for every user -