2 ms·
The article is an okay start, but it wasn't written by an expert. It lists some real pitfalls but doesn't provide commonly accepted solutions. Some examples...
by jeff571 9y ago
The article is an okay start, but it wasn't written by an expert. It lists some real pitfalls but doesn't provide commonly accepted solutions. Some examples...
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...
- colanderman 9y ago({ }) is handy but is a GCC extension. So is "typeof"; "__typeof__" is the standard spelling.
- AstralStorm 9y agoWhich is why C++ has decltype that is standard now.