Checking for whether compiler supports C99 or C++0x

E

er ci

I think I've seen

#ifdef __C99__

before, but is there something similar for C++0x?

Thanks.
 
B

Bo Persson

er said:
I think I've seen

#ifdef __C99__

before, but is there something similar for C++0x?

Thanks.

You the symbol __cplusplus which is defined to be 199711L for
compilers following the 1998 standard, supposedly approved in november
1997.

This symbol will get a new value for C++0x. We just don't know yet
what date it will be - perhaps 201111L ?


Bo Persson
 
Ö

Öö Tiib

I think I've seen

#ifdef __C99__

before, but is there something similar for C++0x?

No compiler supports fully next standard. If you want to write
portable code then avoid all that C++0x for 3-4 years *after* (if it
ever) becomes standard. You will have lot of portability-related fun
without it, that is guaranteed.
 
J

Juha Nieminen

Bo Persson said:
You the symbol __cplusplus which is defined to be 199711L for
compilers following the 1998 standard, supposedly approved in november
1997.

This symbol will get a new value for C++0x. We just don't know yet
what date it will be - perhaps 201111L ?

You could always do an #if (__cplusplus > 199711)
 
E

er

Actually, what would be ideal would be check whether the compiler flag
-std=c++0x has been set. Can compiler flags be "caught" by a pp macro?
 
S

SG

Actually, what would be ideal would be check whether the compiler flag
-std=c++0x has been set. Can compiler flags be "caught" by a pp macro?

In this case (GCC invoked with -std=c++0): Yes. See its documentation
(look for a section called "predefined macros" or something along
these lines).

Cheers!
SG
 
E

er

The built-in macro __cplusplus is the standard way of checking the C++


How exactly, please, as I'm unable to verify this? See below

Under GCC 4.4, with -std=c++0x, under OSX 10.6.4,

#include <iostream>

void f(T&&){} // to be sure that c++0x is enabled,

int main(){

std::cout << "cpp : " << __cplusplus << std::endl; // outputs 1

#if __cplusplus > 199711L
const enable = true;
#else
const enable = false;
#endif

assert( enable ); // fails

return 0;
}
 
E

er

Of course, it should be

const bool enable
   const enable = true;
   #else
   const enable = false;
   #endif

(Since the code is run from a virtual machine, I have to copy it
manually.)
 
S

SG

Under GCC 4.4, with -std=c++0x, under OSX 10.6.4,

   std::cout << "cpp : " << __cplusplus << std::endl; // outputs 1

   #if __cplusplus > 199711L
   const bool enable = true;
   #else
   const bool enable = false;
   #endif

   assert( enable ); // fails

You have been told already that there IS NOT YET an official value the
__cplusplus macro expands to for the next C++ revision. The next
revision is NOT YET DONE.

As for why GCC uses 1 instead of 199711 or how to detect the
experimental C++0x mode, you will find the answers in your compiler's
manual.
http://gcc.gnu.org/onlinedocs/cpp/Predefined-Macros.html

Cheers!
SG
 
E

er

You have been told already that there IS NOT YET an official value the

Yes, but I'm not testing for an exact value, only a half-range. It was
my understanding from previous comments that this would suffice.
Thanks for the links.

Would this be the relevant section for GCC?

3.7.1 Standard Predefined Macros

__cplusplus
This macro is defined when the C++ compiler is in use. You can use
__cplusplus to test whether a header is compiled by a C compiler or a C
++ compiler. This macro is similar to __STDC_VERSION__, in that it
expands to a version number. A fully conforming implementation of the
1998 C++ standard will define this macro to 199711L. The GNU C++
compiler is not yet fully conforming, so it uses 1 instead. It is
hoped to complete the implementation of standard C++ in the near
future.

So I guess GCC is out for checking C++0x

3.7.3 System-specific Predefined Macros

When the -ansi option, or any -std option that requests strict
conformance, is given to the compiler, all the system-specific
predefined macros outside the reserved namespace are suppressed.

What does reserved namespace mean, here?
 
S

SG

So I guess GCC is out for checking C++0x

In addition to Pete's comment: You gotta try much harder. For example,
look for "C++0x" on that page I linked to. RTFM!
What does reserved namespace mean, here?

The answer to that is on the same page you quoted the paragraph from!
RTFM!

Cheers!
SG
 
E

er

In addition to Pete's comment: You gotta try much harder. For example,

Sorry, I wasn't trying too hard anymore, just sharing what caught my
eye as a courtesy to whoever stumbles upon this pb, bec the effort of
finding portable across platforms/compilers solution is not going to
be worth it. For now I'll just manually set a macro to switch between
two implementations, where relevant, one for C03 and the other that
exploits C++0x features.
look for "C++0x" on that page I linked to. RTFM!

You mean this?

__GXX_EXPERIMENTAL_CXX0X__
This macro is defined when compiling a C++ source file with the
option -std=c++0x or -std=gnu++0x. It indicates that some features
likely to be included in C++0x are available. Note that these features
are experimental, and may change or be removed in future versions of
GCC.

Yes, I took note the comments that C++0x is not a finalized product.
The reason I pursue it to be able to switch between two
implementations (see above) as some people already request C++0x
feature, notwithstanding its incompleteness.
The answer to that is on the same page you quoted the paragraph from!
RTFM!

OK, got it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top