C++Language version detection

One of those snippets you just copy-paste from SO? Can I please explain to you a few basics before your copy-paste this one? Let’s suppose you have developed for C++17.

Pre 2020, ISO C++ committee WG21 document N4835 was C++20 draft. Please look it up.

__cplusplus macro in C++20 draft, 2019 Dec

201703L is in there to indicate C++20 value is not declared and defined yet. That 201703L, is a standard C++17 value. Each and every compiler vendor declares 201703L for C++17 for __cplusplus. (Or if ready yet for the C++20 code.)

These macros are for:

  • situations when customers **might** try your app in systems or situations, which are undeclared as your app operating requirements.
    • you have your app operating requirements documented, yes?
  • the situation, when your code needs to know, is it in the C++20 or C++23 mode, now and in the future
  • Etc.

One could indeed simplify and fix the exact C++ version

Let us assume you are developing for C++17. And you do not want your team or teams to accidentally overlap into the C++20 teritory.

Instead of a page long compiler agnostic and OS agnostic “wall of macros” that decides which actual C++ version is in use, one can get hold of only the C++17 value. This is all we might need: To know if are we below the required C++ 17 value or are we above the required mark.

This is the way to demand exactly C++17 and nothing else. And that will work “forever”.

That would indeed cover the operational requirements of the C++17 language requirement.

cl.exe has to be the odd one out

Alas, that does not work right now for developers using  Microsoft cl.exe (c++ compiler). So I had to change it

This is because, for me here __cplusplus == 199711L, and my compiler is

Which was the very latest, at the moment of this writing.

To operate in a standard way,  using CL you need to compile with the /Zc:__cplusplus switch to see the C++ standard defined value of the __cplusplus macro.

And nobody does that.

And what do we do now when C++20 is official?

At that moment  __cplusplus will be having the official value which we will use. Very likely our app operational requirement will then become C++17 or C++20. Ir just C++20.

The __cplusplus value for C++20 is 202002L

NOTE: As of this writing no vendor has 100% full C++20 implemented. Please be aware of that and use feature checking as explained here.

And yes, that would be a proverbial exercise for the reader.

%d bloggers like this: