Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++11 predefined macro

Tags:

Are there any predefined macros for C++ in order the code could identify the standard?

e.g. Currently most compilers puts "array" into "tr1" folder but for C++11 it would be part of STL. So currently

#include <tr1/array> 

but c++11

#include <array> 

What is the predefined macros for 03 standard and 11 standard in order I can use #ifdef to identify?

Also, I suppose there are macros for C90 and C99?

Thanksx

like image 585
xis Avatar asked Aug 28 '11 21:08

xis


People also ask

What are the predefined macros?

Predefined macros are those that the compiler defines (in contrast to those user defines in the source file). Those macros must not be re-defined or undefined by user.

Is file a predefined macro in C?

__FILE__ Macro: __FILE__ macro holds the file name of the currently executing program in the computer. It is also used in debugging, generating error reports and log messages.

What is __ file __ in C?

__FILE__ This macro expands to the name of the current input file, in the form of a C string constant. This is the path by which the preprocessor opened the file, not the short name specified in ' #include ' or as the input file name argument. For example, "/usr/local/include/myheader.


2 Answers

From Stroustrup's C++11 FAQ

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

You can likely test it's value to determine if it's c++0x or not then.

like image 160
jcoder Avatar answered Oct 07 '22 10:10

jcoder


Nitpick...

Your particular issue does not depend on your compiler, it depends on the Standard Library implementation.

Since you are free to pick a different Standard Library that the one provided by your compiler (for example, trying out libc++ or stlport), no amount of compiler specific information will help you here.

Your best bet is therefore to create a specific header file yourself, in which you will choose either one or the other (depending on a build option).

// array.hpp #ifdef STD_HAS_TR1_ARRAY_HEADER #include <tr1/array> #else #include <array> #endif 

You then document the compiler option:

Passing -DSTD_HAS_TR1_ARRAY_HEADER will mean that std::tr1::array is defined in <tr1/array> instead of the default <array>.

And you're done.

like image 37
Matthieu M. Avatar answered Oct 07 '22 09:10

Matthieu M.