2

MSVC does not properly define __cplusplus, unless one specifies the /Z:cplusplus switch.

Now, in a library I'm maintaining, I have some conditional compilation logic such as:

#if __cplusplus >= 201703L
etc. etc.
#endif

Now - as I intend my code to be portable, and MSVC is the popular compiler on Windows - should I "cater" to it by replacing that condition with:

#if (__cplusplus >= 201703L) || (_MSVC_LANG >= 201703L)
etc. etc.
#endif

or should I use the standard facility, and expect library users to use /Z:cplusplus?

einpoklum
  • 118,144
  • 57
  • 340
  • 684
  • `_MSVC_LANG` is less reliable than `__cplusplus`. See https://stackoverflow.com/questions/43639122/which-values-can-msvc-lang-have – 273K May 28 '22 at 19:53
  • @273K: I know, but my check involves both. – einpoklum May 28 '22 at 21:34
  • What you suggest sounds like a good idea to me. How many million edits will that entail? Another, perhaps more future-proof, solution would be to localise that messy test in a single `#include` file and define something in there of your own which you then test inline, as and when needed. So, for example, `#if (__cplusplus >= 201703L) || (_MSVC_LANG >= 201703L) .. #define einpoklum_cpp_ver 17 .. #endif` and then, where needed, `#if einpoklum_cpp_ver >= 17 ... #endif`. – Paul Sanders May 28 '22 at 21:47
  • @PaulSanders: Well, that would still be about the same number of edits; and would be somewhat opaque/mysterious. – einpoklum May 28 '22 at 21:55
  • Sure, but I don't see that it it's necessarily opaque. Just choose a nice descriptive name for your `#define`(s) and you're golden. Put it this way, localising that messy test sounds like a sensible move to me. Suppose you get it wrong and need to change it (or maybe later want to add a second one for C++20 or something). – Paul Sanders May 28 '22 at 21:58
  • Sorry, I can abbreviate that: DRY. And does your IDE have a 'jump to definition' function? Visual Studio certainly does. – Paul Sanders May 28 '22 at 22:21

1 Answers1

2

Instead of __cplusplus, I test the various feature macros,(__cpp_inline_variables as an example). I only use __cplusplus to distinguish C++ from C, not among the various C++ standards.

SoronelHaetir
  • 14,104
  • 1
  • 12
  • 23
  • Won't they depend on the feature-specific headers which I want to understand whether to include or not? – einpoklum May 28 '22 at 21:34
  • He's including `bits/stdc++` :) (sorry, @soronel, I couldn't resist!) – Paul Sanders May 28 '22 at 21:49
  • I use __has_include to check for those. And you can check whether __has_include is supported before actually using it. – SoronelHaetir May 28 '22 at 21:50
  • According to [this page](https://learn.microsoft.com/en-us/cpp/overview/visual-cpp-language-conformance?view=msvc-170) MSVC supports `__has_include` from VS2017 15.3 onwards. – Paul Sanders May 28 '22 at 21:54
  • @SoronelHaetir: So can you edit your answer to present a full example for some specific C++17 feature with an include file? – einpoklum May 28 '22 at 21:56