Is there a preprocessor directive to detect C ++ 11x support?

If you have code in which I would like to use the C ++ 11x extensions as much as possible, but have a backup if this is not supported. Currently, the GCC version for OSX and the VisualC compiler practically do not support C ++ 11x, so I use:

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

And it works, but it's actually not quite right, especially since the gcc compiler in MacPorts supports C ++ 11x.

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

+43
c ++ gcc c-preprocessor preprocessor-directive
May 23 '12 at 9:53 a.m.
source share
3 answers

__cplusplus should be defined as 199711L in pre-C ++ 11, 201103L in those who support C ++ 11. In practice, this is another question: most compilers are only halfway, so you should not define it as 201103L , even if they support features that interest you. And this is not unheard of for the compiler for lie: a compiler that defines it as 199711L and does not support export for templates, for example. But a functional check does not exist.

The simplest solution is to simply not use any specific new function until you are sure that all its compilers support it. You should still write and maintain backup code; why support two versions. The only exception to this rule may be new features that affect performance: whether the compiler supports transfer semantics or not. In such cases, I would suggest including a compiler dependent file that you write based on compiler documentation and personal tests; simply because the compiler can document that it supports a particular function does not mean that its support is not an error. Just create a directory for each target compiler, put this file there and specify the appropriate -I or /I option in the makefile or project file.

And your tests should be something like:

 #ifdef HAS_MOVE_SEMANTICS ... #endif 

not just the compiler, version, or something else.

+43
May 23 '12 at 10:04 a.m.
source share

You can check the value of the __cplusplus macro. For C ++ 11, this is more than 199711L .

So something like

 #if __cplusplus > 199711L #endif 
+24
May 23 '12 at 9:54 a.m.
source share

The Boost.Config library provides granular preprocessor macros that can be used for conditional compilation based on the presence of this C ++ 11 function.

(For the compiler, C ++ 11 support does not have to be an all-or-nothing sentence. For example, consider how Microsoft cherry chose which C ++ 11 features to include in Visual Studio 2012 based on what they think will bring most benefit your customers.)

+7
May 21 '13 at 2:24
source share



All Articles