Are all macros evil? [duplicate]-Collection of common programming errors

I’ll ignore macros that don’t take parameters – as litb says, if you want to tell the preprocessor what code to process, such as in include guards, they’re the business. For many other purposes, a constant does as well.

If you have an idea for a parameterized macro, but realise you can replace it with a function template, then do so. The same goes if you can replace it with a struct template, perhaps using a function as well.

If you can’t replace it with templates (or, to be precise, if you can’t figure out how to do so), then assess whether to use a macro, or just suck it up and write things out longhand each time.

The thing about macros is that you’re mixing two languages (the preprocessor language with “proper” C++). This creates headaches, because the compiler only understands one of them at a time, and so will cheerfully allow function-style macros which behave totally unlike actual function calls, which they syntactically resemble at the point of use. But there are cases where it’s worth it.

One example is the “verbose logging macro”: it does something you can’t do without the preprocessor or messy duplication:

#define LOG(x) (std::cout

Originally posted 2013-11-10 00:46:05.