views:

164

answers:

2

I'm trying to determine if C++0x features are available when compiling. Is there a common preprocessor macro? I'm using Visual Studio 2010's compiler and Intel's compiler.

+3  A: 

The macro __cplusplus will have a value greater than 199711L.

That said, not all compilers will fill this value out. Better to use Roger's solution.

GMan
Do you have a quote from the draft that guarantees that?
anon
Even with `icc -std=c++0x`, \_\_cplusplus is still defined to 1.
Roger Pate
Nope, just of Bjarne's site. I assume what he said would likely be in the standard,though I suppose that may not be the case.
GMan
Neil: best available is only footnote 149, pg 383, "It is intended that future versions of this standard will replace the value of this macro with a greater value." [n2723] (Copied identically from the current standard, 16.8.)
Roger Pate
The real issue is compilers don't use the standard value for this macro, either because they "don't have 100% support" or another reason; and it doesn't even apply when you're talking about partial/experimental support of disparate features from a draft.
Roger Pate
I forsee quite a bit of broken code for people that rely on __cplusplus - not me of course!
anon
I think g++ currently just `#define`s `__cplusplus` to 1 due to a compiler bug on some obscure platform.
KitsuneYMG
+3  A: 

The usual way to do this is determine it in the build system, and pass "configuration macros", commonly named HAS_*, when compiling. For example: compiler -DHAS_LAMBDA source.cpp.

If you can determine this from compiler version macro, then you can define these macros in a configuration header which checks that; however, you won't be able to do this for anything controlled by a command-line option. Your build system does know what options you specify, however, and can use that info.

See boost.config for a real example and lots of details about specific compilers, versions, and features.

Roger Pate