But if -sadly- you must use C, metaprogramming using macros is not a terrible thing.
It is a terrible thing. It's possible to do without them, and you'll like your code better. Your symbolic debugger and syntax directed editor will work properly. The poor schlub who has to fix the bugs in your code after you leave will be grateful. Your spouse will be happy and your children will prosper.
For example,
#define foo(x) ((x) + 1)
replace with: int foo(int x) { return x + 1; }
The compiler will inline it for you. There's no penalty. #define FOO 3
Replace with: enum { FOO = 3 };
or: const int FOO = 3;
Replace: #if FOO == 3
bar();
#endif
with: if (FOO == 3) bar();
The optimizer will delete bar() if FOO is a manifest constant that is not 3.According to this stance, any code that's suppressed by the C preprocessor should either be written in an if {} statement so that it will at least continue to compile as the surrounding code changes, or be replaced with comments describing what it does (or did), if it's important enough to keep track of.
Can't really think of many good counterarguments to this. Machine dependence might be one, but then you could argue that the preprocessor is being used to cover up for an inadequate HAL.
Conditional compilation is an optimization, not a semantic intent.
> non-optimized debug builds are affected
The code size will be larger. Doesn't matter.
The optimizer will delete bar() if FOO is a manifest constant that is not 3.
Yes, but the compiler won't delete it and complain if bar is not defined. if constexpr is not a direct replacement for if macro extern void bar();
will define it to the satisfaction of the compiler. The linker won't complain if the compiler removes the call to it.