125

I have always asked this but I have never received a really good answer; I think that almost any programmer before even writing the first "Hello World" had encountered a phrase like "macro should never be used", "macro are evil" and so on, my question is: why? With the new C++11 is there a real alternative after so many years?

The easy part is about macros like #pragma, that are platform specific and compiler specific, and most of the time they have serious flaws like #pragma once that is error prone in at least 2 important situation: same name in different paths and with some network setups and filesystems.

But in general, what about macros and alternatives to their usage?

sepp2k
  • 363,768
  • 54
  • 674
  • 675
user1849534
  • 2,329
  • 4
  • 18
  • 20

8 Answers8

207

Macros are just like any other tool - a hammer used in a murder is not evil because it's a hammer. It is evil in the way the person uses it in that way. If you want to hammer in nails, a hammer is a perfect tool.

There are a few aspects to macros that make them "bad" (I'll expand on each later, and suggest alternatives):

  1. You can not debug macros.
  2. Macro expansion can lead to strange side effects.
  3. Macros have no "namespace", so if you have a macro that clashes with a name used elsewhere, you get macro replacements where you didn't want it, and this usually leads to strange error messages.
  4. Macros may affect things you don't realize.

So let's expand a little here:

1) Macros can't be debugged. When you have a macro that translates to a number or a string, the source code will have the macro name, and many debuggers can't "see" what the macro translates to. So you don't actually know what is going on.

Replacement: Use enum, const T, or constexpr T

For "function-like" macros, because the debugger works on a "per source line where you are" level, your macro will act like a single statement, no matter if it's one statement or a hundred. Makes it hard to figure out what is going on.

Replacement: Use functions - inline if it needs to be "fast" (but beware that too much inline is not a good thing), and constexpr if it's needed at compile time.

2) Macro expansions can have strange side effects.

The famous one is #define SQUARE(x) ((x) * (x)) and the use x2 = SQUARE(x++). That leads to x2 = (x++) * (x++);, which, even if it was valid code [1], would almost certainly not be what the programmer wanted. If it was a function, it would be fine to do x++, and x would only increment once.

Another example is "if else" in macros, say we have this:

#define safe_divide(res, x, y)   if (y != 0) res = x/y;

and then

if (something) safe_divide(b, a, x);
else printf("Something is not set...");

It actually becomes completely the wrong thing....

Replacement: real functions.

3) Macros have no namespace

If we have a macro:

#define begin() x = 0

and we have some code in C++ that uses begin:

std::vector<int> v;

... stuff is loaded into v ... 

for (std::vector<int>::iterator it = myvector.begin() ; it != myvector.end(); ++it)
   std::cout << ' ' << *it;

Now, what error message do you think you get, and where do you look for an error [assuming you have completely forgotten - or didn't even know about - the begin macro that lives in some header file that someone else wrote? [and even more fun if you included that macro before the <vector> include - you'd be drowning in strange errors that makes absolutely no sense when you look at the code itself.

Replacement: Well there isn't so much as a replacement as a "rule" - only use uppercase names for macros, and never use all uppercase names for other things.

4) Macros have effects you don't realize

Take this function:

#define begin() x = 0
#define end() x = 17
... a few thousand lines of stuff here ... 
void dostuff()
{
    int x = 7;

    begin();

    ... more code using x ... 

    printf("x=%d\n", x);

    end();

}

Now, without looking at the macro, you would think that begin is a function, which shouldn't affect x.

This sort of thing, and I've seen much more complex examples, can REALLY mess up your day!

Replacement: Either don't use a macro to set x, or pass x in as an argument.

There are times when using macros is definitely beneficial. One example is to wrap a function with macros to pass on file/line information:

#define malloc(x) my_debug_malloc(x, __FILE__, __LINE__)
#define free(x)  my_debug_free(x, __FILE__, __LINE__)

Now we can use my_debug_malloc as the regular malloc in the code, but it has extra arguments, so when it comes to the end and we scan the "which memory elements hasn't been freed", we can print where the allocation was made so the programmer can track down the leak.

[1] It is undefined behaviour to update one variable more than once "in a sequence point". A sequence point is not exactly the same as a statement, but for most intents and purposes, that's what we should consider it as. So doing x++ * x++ will update x twice, which is undefined and will probably lead to different values on different systems, and different outcome value in x as well.

Pharap
  • 3,826
  • 5
  • 37
  • 51
Mats Petersson
  • 126,704
  • 14
  • 140
  • 227
  • 6
    The `if else` problems can be solved by wrapping the macro body inside `do { ... } while(0)`. This behaves as one would expect with respect to `if` and `for` and other potentially-risky control-flow issues. But yes, a real function is usually a better solution. `#define macro(arg1) do { int x = func(arg1); func2(x0); } while(0)` – Aaron McDaid Oct 10 '13 at 22:10
  • 16
    @AaronMcDaid: Yes, there are some workarounds that solve some of the problems that are exposed in these macros. The whole point of my post was not to show how to do macros well, but "how easy it is to get macros wrong", where there is a good alternative. That said, there are things that macros solve very easily, and there are times when macros are the right thing to do too. – Mats Petersson Oct 11 '13 at 06:10
  • 1
    In point 3, the errors aren't really a problem anymore. Modern compilers such as Clang will say something like `note: expanded from macro 'begin'` and show where `begin` is defined. – kirbyfan64sos Mar 05 '15 at 20:50
  • @kirbyfan64sos: Yes, Clang is pretty good on that. Not everyone uses clang, and until recently gcc wasn't very good at identifying this scenario. – Mats Petersson Mar 05 '15 at 21:42
  • 6
    Macros are hard to translate to other languages. – Marco van de Voort Mar 01 '16 at 08:29
  • "which, even if it was valid code [1], would " suggests that there should be a footnote elaborating this. I hate when this happens, with asterisks indicating that there's some fine print that applies, but can't be found and so on. It feels like there's something important missing... – skyking Mar 21 '16 at 07:07
  • An important thing to add is that function-like macros, unlike normal functions, don't have strictly defined argument types which can lead to unexpected and unwanted behaviour of the expanded code (mismatching type sizes, signedness, etc.). – ivokabel Jan 25 '17 at 11:48
  • The footnote is rather interesting, can you provide a link to the relevant documentation? Searching only leads here again. – Francesco Dondi Sep 07 '17 at 08:09
  • 1
    @FrancescoDondi: https://stackoverflow.com/questions/4176328/undefined-behavior-and-sequence-points/4176333#4176333 (quite a bit down in that answer, it talks about i++ * i++ and such. – Mats Petersson Sep 09 '17 at 06:03
  • Every single example here is terrible. This is only an answer to "what are some things no sane programmer should ever do in a macro", not to why are macros "evil". The answer to most of those objections are "don't write it that way". – AShelly Dec 10 '18 at 20:21
  • 2
    @AShelly: And what would you call those uses? I've seen just about every one of those things in code that I've "worked on" (not necessarily written by someone I know). Sure, they should not do that, but the fact that those are entirely possible is what I would say is evil about macros. Please feel free to give a suggestion as to how this answer can be made better. – Mats Petersson Dec 11 '18 at 21:38
  • 1
    (Additionally, if you read my first sentence, I explain that macros in themselves are not evil, bad use of macros is what causes problems) – Mats Petersson Dec 11 '18 at 21:44
  • I suggest updating your last paragraph about file/line information, as with C++20 there is finally a better alternative with [source_location](https://en.cppreference.com/w/cpp/utility/source_location) – prapin Nov 19 '21 at 19:19
  • @prapin: if I went through and fixed every answer I've written since 2011-12, to be "latest C++ spec". I'd probably do very little else for the next year... – Mats Petersson Dec 09 '21 at 22:42
  • short of Problem 1 (which I doubt is still particularly relevant today) all of these arguments boil down to PEBCAK – Christoph Fischer Jul 29 '22 at 11:45
26

The saying "macros are evil" usually refers to the use of #define, not #pragma.

Specifically, the expression refers to these two cases:

  • defining magic numbers as macros

  • using macros to replace expressions

with the new C++ 11 there is a real alternative after so many years ?

Yes, for the items in the list above (magic numbers should be defined with const/constexpr and expressions should be defined with [normal/inline/template/inline template] functions.

Here are some of the problems introduced by defining magic numbers as macros and replacind expressions with macros (instead of defining functions for evaluating those expressions):

  • when defining macros for magic numbers, the compiler retains no type information for the defined values. This can cause compilation warnings (and errors) and confuse people debugging the code.

  • when defining macros instead of functions, programmers using that code expect them to work like functions and they do not.

Consider this code:

#define max(a, b) ( ((a) > (b)) ? (a) : (b) )

int a = 5;
int b = 4;

int c = max(++a, b);

You would expect a and c to be 6 after the assignment to c (as it would, with using std::max instead of the macro). Instead, the code performs:

int c = ( ((++a) ? (b)) ? (++a) : (b) ); // after this, c = a = 7

On top of this, macros do not support namespaces, which means that defining macros in your code will limit the client code in what names they can use.

This means that if you define the macro above (for max), you will no longer be able to #include <algorithm> in any of the code below, unless you explicitly write:

#ifdef max
#undef max
#endif
#include <algorithm>

Having macros instead of variables / functions also means that you cannot take their address:

  • if a macro-as-constant evaluates to a magic number, you cannot pass it by address

  • for a macro-as-function, you cannot use it as a predicate or take the function's address or treat it as a functor.

Edit: As an example, the correct alternative to the #define max above:

template<typename T>
inline T max(const T& a, const T& b)
{
    return a > b ? a : b;
}

This does everything the macro does, with one limitation: if the types of the arguments are different, the template version forces you to be explicit (which actually leads to safer, more explicit code):

int a = 0;
double b = 1.;
max(a, b);

If this max is defined as a macro, the code will compile (with a warning).

If this max is defined as a template function, the compiler will point out the ambiguity, and you have to say either max<int>(a, b) or max<double>(a, b) (and thus explicitly state your intent).

Arun
  • 19,750
  • 10
  • 51
  • 60
utnapistim
  • 26,809
  • 3
  • 46
  • 82
  • 1
    It doesn't have to be c++11 specific; you can simply use functions to replace the macros-as-expressions usage and [static] const / constexpr to replace the macros-as-constants usage. – utnapistim Dec 26 '12 at 14:23
  • 1
    Even C99 allows the use of `const int someconstant = 437;`, and it can be used almost every way that a macro would be used. Likewise for small functions. There are a few things where you can write something as a macro that won't work in a regular expression in C (you could make something that averages an array of any type of number, which C can't do - but C++ has templates for that). Whilst C++11 adds a few more things that "you don't need macros for this", it's mostly already solved in earlier C/C++. – Mats Petersson Dec 26 '12 at 14:39
  • Doing a pre-increment while passing an argument is a terrible coding practice. And anyone coding in C/C++ should *not* presume a function-like call is not a macro. – StephenG - Help Ukraine May 14 '18 at 09:29
  • Many implementations voluntarily parenthesize the identifiers `max` and `min` if they are followed by a left parenthesis. But you should not define such macros ... – L. F. Jul 12 '19 at 05:24
14

A common trouble is this :

#define DIV(a,b) a / b

printf("25 / (3+2) = %d", DIV(25,3+2));

It will print 10, not 5, because the preprocessor will expand it this way:

printf("25 / (3+2) = %d", 25 / 3 + 2);

This version is safer:

#define DIV(a,b) (a) / (b)
phaazon
  • 1,972
  • 15
  • 21
  • 2
    interesting example, basically they are just tokens without semantics – user1849534 Dec 26 '12 at 13:53
  • Yes. They are expanded the way they’re given to the macro. The `DIV` macro may be rewritten with a pair of () around `b`. – phaazon Dec 26 '12 at 13:56
  • 2
    You mean `#define DIV(a,b)`, not `#define DIV (a,b)`, which is very different. – rici Dec 26 '12 at 15:43
  • 8
    `#define DIV(a,b) (a) / (b)` is not good enough; as a matter of general practice, always add outermost brackets, like this: `#define DIV(a,b) ( (a) / (b) )` – PJTraill Jun 13 '16 at 21:36
4

Macros are valuable especially for creating generic code (macro's parameters can be anything), sometimes with parameters.

More, this code is placed (ie. inserted) at the point of the macro is used.

OTOH, similar results may be achived with:

  • overloaded functions (different parameter types)

  • templates, in C++ (generic parameter types and values)

  • inline functions (place code where they are called, instead of jumping to a single-point definition -- however, this is rather a recommandation for the compiler).

edit: as for why the macro are bad:

1) no type-checking of the arguments (they have no type), so can be easily misused 2) sometimes expand into very complex code, that can be difficult to identify and understand in the preprocessed file 3) it is easy to make error-prone code in macros, such like:

#define MULTIPLY(a,b) a*b

and then call

MULTIPLY(2+3,4+5)

that expands in

2+3*4+5 (and not into: (2+3)*(4+5)).

To have the latter, you should define:

#define MULTIPLY(a,b) ((a)*(b))
user1284631
  • 4,446
  • 36
  • 61
3

I don't think that there is anything wrong with using preprocessor definitions or macros as you call them.

They are a (meta) language concept found in c/c++ and like any other tool they can make your life easier if you know what you're doing. The trouble with macros is that they are processed before your c/c++ code and generate new code that can be faulty and cause compiler errors which are all but obvious. On the bright side they can help you keep your code clean and save you a lot of typing if used properly, so it comes down to personal preference.

Sandi Hrvić
  • 211
  • 1
  • 1
  • Also, as pointed out by other answers, poorly designed preprocessor definitions can produce code with valid syntax but different semantic meaning which means that compiler will not complain and you introduced a bug in your code that's going to be even harder to find. – Sandi Hrvić Dec 26 '12 at 14:04
3

Macros in C/C++ can serve as an important tool for version control. Same code can be delivered to two clients with a minor configuration of Macros. I use things like

#define IBM_AS_CLIENT
#ifdef IBM_AS_CLIENT 
  #define SOME_VALUE1 X
  #define SOME_VALUE2 Y
#else
  #define SOME_VALUE1 P
  #define SOME_VALUE2 Q
#endif

This kind of functionality is not so easily possible without macros. Macros are actually a great Software Configuration Management Tool and not just a way to create shortcuts for reuse of code. Defining functions for the purpose of reusability in macros can definitely create problems.

indiangarg
  • 51
  • 1
  • Setting Macro values on the cmdline during compilation to build two variants out of one codebase is really nice. in moderation. – Kevin Feb 23 '17 at 00:20
  • 4
    From some perspective, this usage is the most dangerous one: tools (IDEs, static analyzers, refactoring) will have a hard time figuring out the possible code paths. – erenon Feb 25 '19 at 18:07
2

Preprocessor macros are not evil when they are used for intended purposes like:

  • Creating different releases of the same software using #ifdef type of constructs, for example the release of windows for different regions.
  • For defining code testing related values.

Alternatives- One can use some sort of configuration files in ini,xml,json format for similar purposes. But using them will have run time effects on code which a preprocessor macro can avoid.

indiangarg
  • 51
  • 1
  • 1
    since C++17 constexpr if + a header file that contains "config" constexpr variables can replace the #ifdef's. – Enhex Sep 19 '19 at 17:06
1

In my experience macros are not ideal for program size and can be difficult to debug. But if used carefully they are fine.

Often a good alternatives are generic functions and/or inline functions.

Davide Icardi
  • 11,919
  • 8
  • 56
  • 77
  • 3
    What leads you to believe that macros are not well-optimized? They're simple text substitution, and the result is optimized just as much as code written without macros. – Ben Voigt Dec 26 '12 at 13:53
  • @BenVoigt but they don't consider the semantics and this can lead to something that can be considering as "not-optimal" ... at least this is my first tought about that http://stackoverflow.com/a/14041502/1849534 – user1849534 Dec 26 '12 at 13:55
  • 1
    @user1849534: That's not what the word "optimized" means in the context of compilation. – Ben Voigt Dec 26 '12 at 13:56
  • 1
    @BenVoigt Exactly, macros are just text substitution. The compiler just duplicate the code, it is not a performance problem but can increase the program size. Especially true in some contexts where you have program size limitations. Some code are so full with macros that the size of the program is double. – Davide Icardi Dec 26 '12 at 14:04