Macros

Why Not Use Macros in C++?

Macros do plain text replacement, with no type checks, scope or namespaces. Learn the common pitfalls and the modern C++ alternatives, with short examples.

On this page
  1. No type checking
  2. Side effects
  3. No scope or namespaces
  4. Harder to debug
  5. Better alternatives
  6. When macros still make sense

Macros are useful in a few places, but they carry real risks in C++. A macro is text replacement. The preprocessor does not check types, does not respect scope, and does not know about namespaces, so code can change in ways you did not intend. Many common uses have safer alternatives in modern C++.

No type checking

A function-like macro pastes its argument into the text, with no type check and no grouping of the expression:

#define SQUARE(x) x * x

int a = SQUARE(2 + 3);  // expands to 2 + 3 * 2 + 3, which is 11

Adding parentheses fixes the grouping, but the macro still has no types:

#define SQUARE(x) ((x) * (x))

int a = SQUARE(2 + 3);  // 25

An inline function gives the same result with type checking. An argument that cannot convert to int, such as a string, is a compile error:

inline int square(int x) {
    return x * x;
}

int a = square(2 + 3);  // 25

Side effects

The macro pastes its argument in every place it appears, so an argument with side effects runs more than once:

#define SQUARE(x) ((x) * (x))

int i = 2;
int r = SQUARE(i++);  // expands to ((i++) * (i++)): i changes twice

Changing the same variable twice in one expression like this is undefined behavior in C++. A function call evaluates its argument once, so square(i++) increments i once.

No scope or namespaces

A macro replaces every matching name in the code it reaches, whatever the scope or namespace. This can break code that looks unrelated:

#include <algorithm>

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

int main() {
    int biggest = std::max(1, 2);  // the macro rewrites this call, and it does not compile
}

Some Windows headers define min and max as macros unless NOMINMAX is defined, which is a common source of this problem.

Harder to debug

Compiler errors often point to the expanded text rather than the macro call, and that text can be hard to read. To see what the preprocessor produced, run GCC or Clang with -E, or MSVC with /E.

Better alternatives

For most uses, these are safer:

  • Constants. constexpr int kMaxItems = 100; has a type and follows normal scope rules.
  • Small helpers. An inline function or a regular function. A common question is “What is the difference between inline and macro?” The main difference is that an inline function is type-checked and evaluates each argument once.
  • Code for several types. A function template works with several types and still checks them.
  • Named sets of values. An enum class groups related names without leaking them into the surrounding scope.

When macros still make sense

Some uses have no direct replacement:

  • include guards, using #ifndef, #define and #endif
  • conditional compilation, such as #ifdef for platform checks
  • turning a name into text with #, as in stringification

For everything else, the alternatives above are usually the better choice.

The complete guide to macros

How to Stringify a Macro in C++
Read the guide

Keep exploring

Guides from other topics

All topics →

SOLID Principles in C++

Learn the five SOLID principles in C++ with short examples of single responsibility, open/closed, Liskov, interface segregation and dependency inversion.