Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A preprocessor #define to check for language version? C++98/C++03/C++11

Tags:

c++

c++11

Is there a preprocessor definition which I could use in #ifdef checks to discern the different versions of C++ language?

like image 277
ulidtko Avatar asked Dec 16 '11 15:12

ulidtko


People also ask

What is a preprocessor in C language?

The C preprocessor is a macro processor that is used automatically by the C compiler to transform your program before actual compilation. It is called a macro processor because it allows you to define macros, which are brief abbreviations for longer constructs.

What is an example of a preprocessor?

Examples of some preprocessor directives are: #include, #define, #ifndef etc. Remember that the # symbol only provides a path to the preprocessor, and a command such as include is processed by the preprocessor program.

What is a preprocessor answer?

A preprocessor directive is a statement (such as #define) that gives the preprocessor specific instructions on how to modify your source code. The preprocessor is invoked as the first part of your compiler program's compilation step.

Why is preprocessor used?

Preprocessor directives, such as #define and #ifdef , are typically used to make source programs easy to change and easy to compile in different execution environments. Directives in the source file tell the preprocessor to take specific actions.


1 Answers

The value of the __cplusplus macro is supposed to serve this purpose. Unfortunately, GCC has (before 4.7) always set this to 1, making it unusable for this purpose.

(The values are 199711L for C++98/03, and 201103L for C++11.)

like image 111
Kerrek SB Avatar answered Nov 08 '22 07:11

Kerrek SB