GNU C++ how to check when -std=c++0x is in effect?
Asked Answered
G

3

37

My system compiler (gcc42) works fine with the TR1 features that I want, but trying to support newer compiler versions other than the systems, trying to accessing TR1 headers an #error demanding the -std=c++0x option because of how it interfaces with library or some hub bub like that.

/usr/local/lib/gcc45/include/c++/bits/c++0x_warning.h:31:2: error: #error This file requires compiler and library support for the upcoming ISO C++ standard, C++0x. This support is currently experimental, and must be enabled with the -std=c++0x or -std=gnu++0x compiler options.

Having to supply an extra switch is no problem, to support GCC 4.4 and 4.5 under this system (FreeBSD), but obviously it changes the picture!

Using my system compiler (g++ 4.2 default dialect):

#include <tr1/foo>
using std::tr1::foo;

Using newer (4.5) versions of the compiler with -std=c++0x:

#include <foo>
using std::foo;

Is there anyway using the pre processor, that I can tell if g++ is running with C++0x features enabled?

Something like this is what I'm looking for:

#ifdef __CXX0X_MODE__
#endif

but I have not found anything in the manual or off the web.

At this rate, I'm starting to think that life would just be easier, to use Boost as a dependency, and not worry about a new language standard arriving before TR4... hehe.

Gurgle answered 2/6, 2010 at 13:59 Comment(0)
A
37

If you compile with -std=c++0x, then __GXX_EXPERIMENTAL_CXX0X__ will be defined.

Appendage answered 2/6, 2010 at 14:0 Comment(2)
Till GCC adds in the new cplusplus defined the most portable options would be to add '#if defined(__GXX_EXPERIMENTAL_CXX0X) || __cplusplus >= 201103L'Muzz
Is the same for -std=c++11 and GCC4.8.1?Tysontyumen
B
83

There seems, with gcc 4.4.4, to be only one predefined macro hinting that -std=c++0x is in effect:

#define __GXX_EXPERIMENTAL_CXX0X__ 1

I don't have access to gcc 4.5.0 , but you can check that one yourself:

[16:13:41 0 ~] $ g++ -E -dM -std=c++0x -x c++ /dev/null >b
[16:13:44 0 ~] $ g++ -E -dM -std=c++98 -x c++ /dev/null >a
[16:13:50 0 ~] $ diff -u a b
--- a   2010-06-02 16:13:50.200787591 +0200
+++ b   2010-06-02 16:13:44.456912378 +0200
@@ -20,6 +20,7 @@
 #define __linux 1
 #define __DEC32_EPSILON__ 1E-6DF
 #define __unix 1
+#define __GXX_EXPERIMENTAL_CXX0X__ 1
 #define __LDBL_MAX_EXP__ 16384
 #define __linux__ 1
 #define __SCHAR_MAX__ 127

For one-line command do,

g++ -E -dM -std=c++98 -x c++ /dev/null > std1 && g++ -E -dM -std=c++0x -x c++ /dev/null > std2 && diff -u std1 std2 | grep '[+|-]^*#define' && rm std1 std2

gives you something like:

+#define __GXX_EXPERIMENTAL_CXX0X__ 1
Berky answered 2/6, 2010 at 14:14 Comment(3)
+1: “Give a man a fish; you have fed him for today. Teach a man to fish; and you will not have to listen to his incessant whining about how hungry he is.” :)Wendel
You might have to listen to fish stories where ->| |<- is an inch.Barbiturism
You can use bash process substitution to avoid using std1 and std2: diff -u <(g++ -E -dM -std=c++98 -x c++ /dev/null) <(g++ -E -dM -std=c++0x -x c++ /dev/null)Dipsomaniac
A
37

If you compile with -std=c++0x, then __GXX_EXPERIMENTAL_CXX0X__ will be defined.

Appendage answered 2/6, 2010 at 14:0 Comment(2)
Till GCC adds in the new cplusplus defined the most portable options would be to add '#if defined(__GXX_EXPERIMENTAL_CXX0X) || __cplusplus >= 201103L'Muzz
Is the same for -std=c++11 and GCC4.8.1?Tysontyumen
B
17

Well, from gcc-4.7 onwards you'll be able to check __cplusplus:

"G++ now sets the predefined macro __cplusplus to the correct value, 199711L for C++98/03, and 201103L for C++11"

This should be the correct, standard-compliant way to do it. Unfortunately, it doesn't work for most gcc installed in the wild.

Barbiturism answered 18/4, 2012 at 15:20 Comment(2)
FWIW this comes from C++11 16.8/1.Eme
@LightnessRacesinOrbit Yes, you're right that this feature is not specifically gcc but is C++ standard. Unfortunately, because of some issue on one of gcc targets we couldn't define __cplusplus sensibly even for C++03/98. I think the target was deprecated finally and we could move on.Barbiturism

© 2022 - 2024 — McMap. All rights reserved.