C++ Reference ยท Preprocessor, Tools and Concepts
Conditional compilation: #if, #ifdef, #ifndef
Reference for C++ conditional compilation: #if, #elif, #else, #endif, #ifdef, #ifndef, defined(), #elifdef, #error and #warning, with practical examples.
The conditional directives let the preprocessor include or skip whole regions of source code depending on macros and constant expressions. Code in a skipped region is not compiled at all: it only has to consist of valid preprocessing tokens, not valid C++. This is how a single code base adapts to different platforms, compilers, build types and optional features.
#if and #elif test an integer constant expression (macros are expanded first, and any leftover identifier counts as 0). #ifdef NAME and #ifndef NAME test only whether a macro is defined. Every conditional block ends with #endif. Since C++23, #elifdef and #elifndef are the matching shortcuts for #elif defined(...).
#if defined(DEBUG) && DEBUG > 1 // verbose logging #elif defined(DEBUG) // light logging #else // nothing #endif
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.
Fail-fast checks use #error (stops compilation) and #warning (C++23, a message; a common extension earlier). They are read-only here because they would stop or clutter the build:
#if !defined(__cplusplus) || __cplusplus < 202002L #error "This project needs C++20 or newer" #endif
Syntax
#if constant-expression #elif constant-expression #else #endif #ifdef identifier // same as #if defined(identifier) #ifndef identifier // same as #if !defined(identifier) #elifdef identifier // C++23 #elifndef identifier // C++23 #error message #warning message // C++23
| Piece | Description |
|---|---|
defined(X) or defined X |
1 if macro X is defined, else 0. Only valid inside #if/#elif. |
__has_include(h) |
1 if the header exists. |
__has_cpp_attribute(a) |
Non-zero if the attribute is supported (the value is the year/month it was added). |
| Operators | Integer arithmetic, comparisons, &&, ` |
Errors: every #if/#ifdef needs a matching #endif in the same file, or compilation fails. Floating-point or string expressions in #if are errors.
Usage notes
#ifversus#ifdef:#ifdef Xis true whenXis defined even as#define X 0;#if Xwould then be false. Pick one convention per macro: "defined or not" (feature flags) or "always defined, value 0/1" (so typos can be caught with-Wundef).- Use
definedwith care. Writing#if defined(X) && X > 2avoids relying on undefined names evaluating to 0. #ifis notif. It cannot see variables,constexprvalues orsizeof. For compile-time decisions based on C++ entities useif constexprorstatic_assert; it keeps all branches type-checked.- Prefer
if constexprover#ifwhen the choice depends on a template parameter or a type property. - Indent directives (
# if) for nested blocks to keep them readable, and comment the closing line:#endif // USE_COLOR. - Too many variants make code untestable: each combination must compile. Keep the number of switches small and test all builds in CI.
- Platform macros such as
_WIN32,__linux__,__APPLE__,__GNUC__,_MSC_VERare compiler-defined; wrap them in your own named macro once, then use that. NDEBUGis the standard macro that disablesassert. Build types set it with-DNDEBUG.- Include guards use this mechanism:
#ifndef FOO_H...#endif. - Shadowed code rots. Code under a rarely enabled
#ifoften stops compiling. Prefer a runtime orif constexprswitch where practical.