📅 2020-Sep-19 ⬩ ✍️ Ashwin Nanjappa ⬩ 🏷️ cpp, macro ⬩ 📚 Archive
The C++ language specifies that the macro __cplusplus
will be defined if a C++ compiler is being used to compile the code. This is a good way to compile C or C++ code depending on whether a C or a C++ compiler is being used. This can be used for example to hide C++ constructs in a header file that is shared between C and C++ source files.
For example:
#ifdef __cplusplus
// Write some C++ code here
#else
// Write some C code here
#endif
The value of the __cplusplus
macro is set by the compiler based on the version of the C++ standard it supports or you are requesting it to apply.
Here is a list of C++ standards and their corresponding __cplusplus
values:
199711L
201103L
201402L
201703L
202002L
This is a value set inside the compiler implementation and not in any C++ standard library header file. I found the above values in the GCC source code defined here.