How do I check for C++20 support? What is the value of __cplusplus for C++20?
It's too early for that.
Until the standard replaces it, use:
#if __cplusplus > 201703L
// C++20 code
#endif
since the predefined macro of C++20 is going to be larger than the one of C++17.
As @SombreroChicken's answer mentions, [cpp.predefined] (1.1) specifies (emphasis mine):
__cplusplus
The integer literal
201703L
. [Note: It is intended that future versions of this International Standard will replace the value of this macro with a greater value.]
The macros used, as of Nov 2018, are:
- GCC 9.0.0:
201709L
for C++2a. Live demo - Clang 8.0.0:
201707L
. Live demo - VC++ 15.9.3:
201704L
(as @Acorn's answer mentions).
PS: If you are interested in specific features, then [cpp.predefined] (1.8) defines corresponding macros, which you could use. Notice though, that they might change in the future.
The value for C++20 is 202002L
, as you can see at [cpp.predefined]p1.1:
__cplusplus
The integer literal
202002L
. [ Note: It is intended that future versions of this International Standard will replace the value of this macro with a greater value. — end note ]
Therefore, for compilers that already implement the new standard, you can check by:
#if __cplusplus >= 202002L
// C++20 (and later) code
#endif
As of 2020-09-04, this is the compiler support:
- Clang >= 10
- GCC: No (as of trunk).
- MSVC: No (as of version 19.27, note that it will require
/Zc:__cplusplus
). - ICC: No (as of version 19.0.1).
For those that do not implement it yet, you can instead use:
#if __cplusplus > 201703L
// C++20 (and later) code
#endif
Since all compilers define it already higher than C++17's 201703L
in their respective "C++ latest" mode.
There's no known __cplusplus
version yet because C++20 is still in development. There are only drafts for C++20.
The latest draft N4788 still contains:
__cplusplus
The integer literal
201703L
. [Note: It is intended that future versions of this International Standard will replace the value of this macro with a greater value. —end note]
As for checking it, I would use @gsamaras answer.