Is there a preprocessor directive for detecting C++11x support?

Kenneth picture Kenneth · May 23, 2012 · Viewed 43.3k times · Source

If have some code where I would like to use C++11x extensions as much as possible, but have a fallback if this is not supported. Currently the OSX version of GCC and the VisualC compiler has little to no support for C++11x, so I use:

#if (defined(__APPLE__) || (defined(_WIN32)))
   ...fallback code without C++11x ...
#else
   ... code using C++11x ...
#endif

And this works, but is not really the right thing to do, especially since the gcc compiler in MacPorts DOES support c++11x.

Is there a #define C11X_SUPPORTED type macro? Perhaps something only GCC has?

Answer

James Kanze picture James Kanze · May 23, 2012

__cplusplus should be defined as 199711L in pre-C++11 compilers, 201103L in those suporting C++11. Whether this is much help in practice is another question: most compilers are only halfway there, so shouldn't be defining it as 201103L, even if they support the features you are interested in. And it's not unheard of for a compiler to lie: a compiler which defines it as 199711L and doesn't support export for templates, for example. But there's no standard feature by feature test.

The simplest solution is just not to use any particular new feature until you can be sure that all compilers support it. You have to write and support the fallback code anyway; why maintain two versions. The one exception to this rule might be new features which impact on performance: whether the compiler supports move semantics or not. In such cases, I'd suggest a compiler dependent include file, which you write yourself based on the compiler documentation and personal tests; just because the compiler may document that it supports a specific feature doesn't mean that its support is bug-free. Just create a directory per targeted compiler, put this file there and specify the appropriate -I or /I option in your makefile or project file.

And your tests should be something along the lines of:

#ifdef HAS_MOVE_SEMANTICS
...
#endif

rather than just on the compiler, version or whatever.