Macro __cplusplus Macro __cplusplus Macro __cplusplus
Use Case: Check Compiler Setting Check Compiler Setting
#ifndef __cplusplus #error C++ is required #elif __cplusplus > 199711 // C++11 (or newer) code goes here #else // old-school code goes here #endif ISO Standard Values
| C++98 | #define __cplusplus |
| C++11 | #define __cplusplus |
| C++14 | #define __cplusplus |
| C++17 | #define __cplusplus |
| C++20 | #define __cplusplus |
Compiler Specifics
Older Compilers
If a compiler was released between C++ standards, values for __cplusplus might differ from the ones in the table above. However, you can always rely on the fact that the value of __cplusplus for a newer standard will be strictly larger than that of the previous standard.
Microsoft Visual Studio (MSVC)
- used to use non-standard conforming values
- compiler switch
/Zc:__cplusplus(as of Visual Studio 2017 version 15.7) makes MSVC use the standard values
GCC (g++), Clang (clang++)
use mostly standard conforming values for __cplusplus.
Comments…