Re:How to properly use macros in C
by colanderman on Sunday December 10, 2017 @06:07PM
(#15893986)
({ }) is handy but is a GCC extension.
So is "typeof"; "__typeof__" is the standard spelling.
Re:How to properly use macros in C
by AstralStorm on Monday December 11, 2017 @02:22AM
(#15895637)
Which is why C++ has decltype that is standard now.
multiple lines: wrap in do { } while (0) - can use a multiline macro like a function call, terminated with ; or ({foo; bar;}) - multiple lines evaluate to 'bar'...
Function calls: - in general anything with side effects could be harmful if evaluated more than once. - But possible to write MIN()/MAX() with single evaluation of 'a' and 'b'. #define MAX(a, b) \ ({typeof(a) _a = (a); typeof(b) _b = (b); _a > _b ? _a : _b;})
And other important things like stringify, variadics, etc...