58

Related to questions How do I check for C++11 support? and What is the value of __cplusplus for C++17?

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?

gsamaras
  • 71,951
  • 46
  • 188
  • 305
user2296653
  • 1,151
  • 1
  • 8
  • 17

3 Answers3

51

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)
Acorn
  • 24,970
  • 5
  • 40
  • 69
  • Surely you want `>=` for the 202002 test? Hopefully C++23 will not break *most* C++20 features… – Davis Herring Mar 26 '20 at 18:26
  • 1
    > 201703L will not be sufficient. GCC 8 for instance uses 201709 and it's partial C++2a support. – Vinícius Ferrão Oct 05 '21 at 23:06
  • @ViníciusFerrão I'll clarify. Edit: I removed it entirely, it is already 2021... – Acorn Oct 06 '21 at 17:06
  • @ViníciusFerrão np! – Acorn Oct 06 '21 at 17:11
  • It should be noted that for Microsoft C++ compiler testing `__cplusplus` is useless, because you cannot be sure that whoever is using your code is going to set `/Zc:__cplusplus` compiler switch or even have it available in their compiler version and without that switch `__cplusplus` will always return the same version (`199711L`). The only way to reliably test on Microsoft C++ compiler is to use `_MSVC_LANG` macro which didn't exist before VS 2015 Update 3 so you also have to test for that compiler version or greater. – Igor Levicki Apr 12 '22 at 10:14
40

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.

gsamaras
  • 71,951
  • 46
  • 188
  • 305
  • 6
    gcc 8.2 uses `201709` for `c++2a` and clang 7.0 uses `201707` – bolov Nov 30 '18 at 12:47
  • Same for GCC 9 and Clang 8 @bolov, updated my answer, thanks! – gsamaras Nov 30 '18 at 12:56
  • It is not nice to copy other answers after you already have the accepted and top-voted answer :-) – Acorn Nov 30 '18 at 17:29
  • Yes I agree @Acorn. However, notice how some people are improving their answers in parallel. I see that our answers have a lot in common, just like yours with Sombrero Chicken. If you think, I should modify my answer to improve the gain the future user will have from reading this, then let me know. In any case, if you think yours should be accepted instead, then no problem, your answer is very good :) Also let's clean these up later, for the sake of the future user please. – gsamaras Nov 30 '18 at 18:49
7

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.

Community
  • 1
  • 1
Hatted Rooster
  • 35,759
  • 6
  • 62
  • 122