user7698505
user7698505

Reputation:

How to detect if VS C++ compiler supports C++11?

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

Upvotes: 3

Views: 464

Answers (2)

phuclv
phuclv

Reputation: 41753

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

Upvotes: 0

darune
darune

Reputation: 10962

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

Upvotes: 2

Related Questions