C++17 language version detection

One of those snippets you just copy-paste. Can I please explain to you a few basics before your copy-paste this one?

ISO C++ committee WG21 document N4835 is 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 standard C++17 value. Each and every compiler vendor declares 201703L for C++17 for __cplusplus. (Not so, 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 you app operating requirements, yes?
  • situation, when your code needs to know, is it in the C++20 or C++23 mode, in the future
  • Etc.

One could indeed simplify and fix on C++17

Instead of a page long “wall of macros” that decides which C++ version is on, one can get hold of only the C++17 value. This is all we might need, are we bellow 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”.

#ifndef __cplusplus
#error YOUR_APP requires C++ compiler

#if ( __cplusplus < 201703L ) 
#error YOUR_APP requires the standard C++17 compiler 

#if ( __cplusplus > 201703L )
#error YOUR_APP is not ready yet for the standard C++20 (or higher)

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

#ifndef __cplusplus
#error MY APP requires C++ compiler

#if defined(_MSVC_LANG) && !defined(__clang__)
#define MY_CPLUSPLUS (_MSC_VER == 1900 ? 201103L : _MSVC_LANG)
#define MY_CPLUSPLUS __cplusplus

#if (MY_CPLUSPLUS < 201703L) 
#error MY APP requires the standard C++17 compiler 

#if (MY_CPLUSPLUS > 201703L)
#error MY APP is not ready yet for the standard C++20 (or higher)

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

Microsoft (R) C/C++ Optimizing Compiler Version 19.24.28314 for x86
Copyright (C) Microsoft Corporation. All rights reserved.

Which is the very latest.

Using CL you need to compile with the /Zc:__cplusplus switch to see the C++ standard defined value of the __cplusplus macro.  But nobody does that.

And what we do when C++20 becomes 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.

And yes, that would be an exercise for the reader.

Administrator of an Legacy farm, at work.