Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How are the __cplusplus directive defined in various compilers?

My compiler expands it to 199711L. What does that mean? I read that __cplusplus > 199711L signifies C++11. What are the possible expansions of this macro and what does it signify?

like image 521
unj2 Avatar asked Jun 15 '12 15:06

unj2


People also ask

Where is __ cplusplus defined?

Just compile it with a C++ compiler and __cplusplus is defined automatically in that case. Show activity on this post. The C++ Standard enforces that __cplusplus will always be defined in C++ programs. The C Standard obviously does not.

What is define directive in C++?

The #define directive causes the compiler to substitute token-string for each occurrence of identifier in the source file. The identifier is replaced only when it forms a token. That is, identifier is not replaced if it appears in a comment, in a string, or as part of a longer identifier.

Is __ cplusplus standard?

The __cplusplus preprocessor macro is defined if the compilation unit is compiled with a C++ compiler. Its value corresponds to the C++ standard that the compiler uses to compile a compilation unit.

How are macros defined in CPP?

A macro is a piece of code in a program that is replaced by the value of the macro. Macro is defined by #define directive. Whenever a macro name is encountered by the compiler, it replaces the name with the definition of the macro. Macro definitions need not be terminated by a semi-colon(;).


2 Answers

The 199711L stands for Year=1997, Month = 11 (i.e., November of 1997) -- the date when the committee approved the standard that the rest of the ISO approved in early 1998.

For the 2003 standard, there were few enough changes that the committee (apparently) decided to leave that value unchanged.

For the 2011 standard, it's required to be defined as 201103L, (again, year=2011, month = 03) again meaning that the committee approved the standard as finalized in March of 2011.

For the 2014 standard, it's required to be defined as 201402L, interpreted the same way as above (February 2014).

For the 2017 standard, it's required to be defined as 201703L (March 2017).

For the 2020 standard, the value has been updated to 202002L (February 2020).

Before the original standard was approved, quite a few compilers normally defined it to 0 (or just an empty definition like #define __cplusplus) to signify "not-conforming". When asked for their strictest conformance, many defined it to 1.

I almost forgot to mention, but one more tidbit about ancient compilers: a few of the earliest versions of cfront (and probably a few others copying it) defined c_plusplus instead of __cplusplus. I don't recall it's being defined to any meaningful value though.

like image 98
Jerry Coffin Avatar answered Sep 17 '22 05:09

Jerry Coffin


I think preprocessor defines for the various versions should go into SD-6:

#define __cpp_1997 199711L #define __cpp_2003 __cpp_1997 #define __cpp_2011 201103L #define __cpp_2014 201402L #define __cpp_2017 201703L 

Looking into the 2020s we might have three more standards. I don't doubt that many implementors will have code supporting standards from 1997 onwards.

I for one would like a mnemonic define so I won't have to keep coming back to this post.

like image 44
emsr Avatar answered Sep 20 '22 05:09

emsr