3

How to detect if Visual Studio (VS) C++ compiler supports C++11 through preprocessor macros? I tried with __cplusplus (the preprocessor macro that many people advise to use for this kind of checks) but it fails with VS C++ 2010 compiler (i.e. function get_dimension is never declared):

#if __cplusplus > 199711L
    int get_dimension(int index);
#endif
phuclv
  • 37,963
  • 15
  • 156
  • 475
  • 2
    Perhaps a better question would be why are you using a 10-year old Visual Studio version? – Panagiotis Kanavos Jun 25 '20 at 11:58
  • 2
    @PanagiotisKanavos: perhaps... but it does not invalidate the original question posted here! :) –  Jun 25 '20 at 11:59
  • 5
    What do you mean "it fails". Isn't it supposed to not declare that function for compilers that don't support C++11? Seems like a success to me. – eerorika Jun 25 '20 at 11:59
  • Here is a related question: https://stackoverflow.com/questions/5047971/how-do-i-check-for-c11-support – A.Hristov Jun 25 '20 at 12:00
  • 8
    Why would you expect a compiler released in 2010 to fully support C++11 (formalised in 2011)? – Richard Critten Jun 25 '20 at 12:03
  • @PanagiotisKanavos: most of the time, because the client use a 10-year old version. In my case, our client requires compatibility with gcc 4.1.* ;-) – Caduchon Jun 25 '20 at 12:40
  • Maybe this can help: https://stackoverflow.com/q/10984442/3378179 – Caduchon Jun 25 '20 at 12:42
  • @Caduchon quite often that happens because the client doesn't know that newer versions are actually free, eg by downloading a fresh Windows SDK, Visual Studio Code, or a Community version of Visual Studio. Or are afraid that newer C++ versions will break older code and don't want to test – Panagiotis Kanavos Jun 25 '20 at 12:47
  • @PanagiotisKanavos, yeah. But it could be difficult to change in a short time. In our case, they require compatibility with gcc4.1 for cross-platform comilation on all linux configurations (if I well understood) of a sofware where we add a library. – Caduchon Jun 25 '20 at 12:49

2 Answers2

2

You can check with _MSVC_LANG macro out of the box.

__cplusplus is the language multi compiler ivory tower kind of solution, but unfortunately has to be enabled in MSVC before it can be used meaningfully (and may not be supported in very old versions). This is fantastic for people using eg. gcc where its set up with the version by default (and most of those people will believe it to work on MSVC as well). So only if you need to support many compilers would I worry, and even then I would consider to add special check for some compilers, namely MSVC.

C++ team blog

darune
  • 10,480
  • 2
  • 24
  • 62
  • 2
    And for their current compiler, it should be fine: [MSVC now correctly reports `__cplusplus`](https://devblogs.microsoft.com/cppblog/msvc-now-correctly-reports-__cplusplus/) – Ted Lyngmo Jun 25 '20 at 12:15
  • @TedLyngmo good point, and good to hear - unfortunately it does not help alot currently because it doesn't go back in time - maybe in 5 or 10 years. – darune Jun 25 '20 at 12:20
  • Indeed - we'll have to live with it for a bit :) – Ted Lyngmo Jun 25 '20 at 12:32
0

You need to use #if __cplusplus >= 201103L instead to check if a compiler is 100% C++11 compliant. If it's false then the compiler doesn't support C++11 or only supports a subset of it

Now if you just need to use some specific features in C++11 then you can use Boost to check it. For example if you need constexpr support then use

#ifndef BOOST_NO_CXX11_CONSTEXPR

You can also use some macros that allow the use of C++11 features with C++03 compilers like BOOST_CONSTEXPR

But the better solution is to exclude ancient compilers completely with _MSC_VER or _MSC_FULL_VER

#if _MSC_VER > 1600
phuclv
  • 37,963
  • 15
  • 156
  • 475