In C++, many developers (perhaps even most of us) dislike macros and try to avoid them in favor of proper language constructs. And - it seems the language tends to be encouarging this as well. Gradually, we've been able to get rid of macro use in a lot of cases:
- Type polymorphism -> template programming
- Conditional compilation -> compile-time evaluation of template parameters, SFINAE, etc.
#include
guards ->#pragma once
for some compilers, although I guess you can't trust that; soon we should have C++17 Modules andimport
instead ofinclude
.
My question is - what's left? What kinds of macro use are completely unavoidable, or very painful to avoid? The main examples I can think of is:
Using filenames, line numbers and function/method names:
#define LOG(whatever, ...) log(__FUNCTION__, __FILE__, __LINE__, whatever, VA_ARGS)
Syntactic sugar for taking a block of code as an "argument", e.g.
AT_SCOPE_EXIT { release_resource(); }
in Andrei Alexandrescu's ScopeGuard (or check out the video). Although I guess we don't really need to use the terse macro-using pseudo-syntax for that.
- Perhaps certain kinds of Boost voodoo?
- Backwards compatibility?
What other significant uses of macros am I missing? (No super-specific corner cases please.)