How do I check for C++20 support? What is the value of __cplusplus for C++20?

C++MacrosC++20

C++ Problem Overview


Related to questions How do I check for C++11 support? and https://stackoverflow.com/questions/38456127/what-is-the-value-of-cplusplus-for-c17

How can I inquire whether the compiler can handle / is set up to use C++20? I know that it is, in principle, possible to inquire the C++ version by:

#if __cplusplus > ???
  // C++20 code here
#endif

What should ??? be for C++20?

C++ Solutions


Solution 1 - C++

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.

Solution 2 - C++

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

This is the compiler support so far:

  • Clang >= 10
  • GCC >= 11
  • MSVC >= 19.29 (requires /Zc:__cplusplus)
  • ICX >= 2021
  • ICC: No (version >= 2021 defines 202000L; notice the 0)

Solution 3 - C++

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser2296653View Question on Stackoverflow
Solution 1 - C++gsamarasView Answer on Stackoverflow
Solution 2 - C++AcornView Answer on Stackoverflow
Solution 3 - C++Hatted RoosterView Answer on Stackoverflow