2

Possible Duplicate:
How to determine the version of the C++ standard used by the compiler?

Is there a way for your program to determine this at compile time? Are there preprocessor macros you can use?

I know that g++ has this nice matrix of c++0x/c++11 feature support. Does this exist for other popular compilers? Are there any standard (de facto or otherwise) ways to use the preprocessor to test for the existence of a given feature?

There is a question about this, but the answers do not seem very complete: How to determine the version of the C++ standard used by the compiler?

I know Boost.Config does this, but how does it do it? It might be nice to know if I don't want to use Boost for whatever reason.

I'm closing this as a duplicate because someone edited one of the answers in the original question to be much more complete.

Community
  • 1
  • 1
Omnifarious
  • 54,333
  • 19
  • 131
  • 194
  • @Bo Persson - I'm tempted to delete this question myself in frustration. That question has no answers that are particularly useful. Heck, I found a more useful answer in a g++ bug report than in that question. And nobody is bothering with any decent answers here either. – Omnifarious Aug 20 '11 at 15:19
  • @Omnifarious - I've added a link to a feature matrix, similar to the one boost will be using for boost.config. – Flexo Aug 20 '11 at 15:22
  • I don't think there is any language standard answers, just compiler manuals documenting the amount of C++11 support. The committee specifically rejected the idea of having macros for partial implementations. They want compilers to implement the whole language! – Bo Persson Aug 20 '11 at 15:23
  • 1
    Ahaha.. [third time today...](http://stackoverflow.com/questions/2324658/how-to-determine-the-version-of-the-c-standard-used-by-the-compiler) – Lightness Races in Orbit Aug 20 '11 at 15:36
  • @Tomalak - with the press generated by the C++0x vote it might well not be the last... – Flexo Aug 20 '11 at 16:34
  • @Omnifarious I tried making my answer to the proposed dup as comprehensive as possible. Unfortunately checking for features in a static language is messy. – pmr Aug 20 '11 at 16:45
  • @pmr - I'll close it as a dup then, though I'm going to edit in my bit about what values of `__cplusplus` mean what. – Omnifarious Aug 20 '11 at 17:54
  • @awoodland: I'm certain that it won't be. That's why I was trying to start a comprehensive post on the issue, but I failed. :( – Lightness Races in Orbit Aug 20 '11 at 18:06
  • @Tomalak: Where is your question? I find neither this question, nor the question linked to to have particularly satisfactory answers. I realize it's not an easy problem, but it would be nice to have more details on particular compilers and things. – Omnifarious Aug 20 '11 at 20:01

2 Answers2

1

Look at the value of __cplusplus

In C++0x the macro __cplusplus will be set to a value that differs from (is greater than) the current 199711L.

http://predef.sf.net has some values for detecting specific standards.

The Boost.Config recommendation on the question you linked to is good advice too, and offers a more fine-grained answer. I think it basically codifies a feature matrix and periodically updates it.

Community
  • 1
  • 1
Flexo
  • 87,323
  • 22
  • 191
  • 272
  • Which values correspond to which standards? – Omnifarious Aug 20 '11 at 15:02
  • And, as a practical matter (and this is a bug in g++) g++ defines this macro to be `1` even if you specify `-std=c++0x` on the command line. – Omnifarious Aug 20 '11 at 15:08
  • that may be an acknowledgement that their support is incomplete. It makes Boost.Config all the more valuable though. – Flexo Aug 20 '11 at 15:14
  • https://wiki.apache.org/stdcxx/C%2B%2B0xCompilerSupport has a useful feature matrix too. – Flexo Aug 20 '11 at 15:19
  • The Apache wiki is exactly the sort of thing I'm looking for. – Omnifarious Aug 20 '11 at 15:26
  • If you're prepared to do it at configure instead of build time there's a few relevant ones at https://www.gnu.org/software/autoconf-archive/The-Macros.html and I'm sure CMake has equivalent too. Doing it that way at least has the advantage that it reduces the maintenance burdens of a feature matrix approach. – Flexo Aug 20 '11 at 15:30
  • @pmr - That is true. Unfortunately I can't get 4.7.0 unless I'm willing to compile a snapshot of an unstable tree, or directly from a Subversion repository. :-/ – Omnifarious Aug 20 '11 at 16:27
  • @Omnifarius So, you really are out of options. Boost.Config is basically the only way you can do this. Ah, btw there is a nice matrix for major compilers here: https://wiki.apache.org/stdcxx/C%2B%2B0xCompilerSupport – pmr Aug 20 '11 at 16:38
1

To jump start this in the direction I was hoping it would go, here is some useful information I gleaned from a g++ bug report.

  • C++ pre-C++98: __cplusplus is 1.
  • C++98: __cplusplus is 199711L.
  • C++0x/11: __cplusplus is 201103L.
Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
Omnifarious
  • 54,333
  • 19
  • 131
  • 194