Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the value of __cplusplus for C++17?

Tags:

c++

macros

c++17

We are trying to test some code under C++17 and its change to std::uncaught_exception. I can't seem to get GCC to provide the value of __cplusplus:

$ /opt/local/bin/g++ -std=c++17 -dM -E - </dev/null | grep __cplusplus cc1: warning: command line option '-std=c++1z' is valid for C++/ObjC++ but not for C $ 

And:

$ /opt/local/bin/g++ --version g++-mp-6 (MacPorts gcc6 6.1.0_0) 6.1.0 Copyright (C) 2016 Free Software Foundation, Inc. 

What is the value of __cplusplus when using C++17?

like image 555
jww Avatar asked Jul 19 '16 10:07

jww


People also ask

What does __ cplusplus do?

The C++ compiler, according with the extern "C" ( __cplusplus is defined) directive: Produces a standard C DLL (i.e. the function use the C language mangling scheme) when building th DLL .

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.

How do I enable C++17 in G ++?

C++17 features are available since GCC 5. This mode is the default in GCC 11; it can be explicitly selected with the -std=c++17 command-line flag, or -std=gnu++17 to enable GNU extensions as well.

What was added in C++17?

C++17 includes the following new language features: template argument deduction for class templates. declaring non-type template parameters with auto. folding expressions.


2 Answers

tl;dr: For C++17, __cplusplus is 201703L.

What is the value of __cplusplus when using C++17?

According to the draft standard N4594 §16.8/p1 Predefined macro names [cpp.predefined] (Emphasis Mine):

The following macro names shall be defined by the implementation: __cplusplus The name __cplusplus is defined to the value 201402L when compiling a C++ translation unit.156

156) It is intended that future versions of this standard will replace the value of this macro with a greater value. Non-conforming compilers should use a value with at most five decimal digits.

However the same value is appointed for the C++14 standard. Apparently it seems so, that there's no official/standard __cplusplus value set yet for the C++17 standard.

In GCC versions 6.1 and 7.0 the value is changed to 201500

Live Demo

In Clang version 3.8 and 3.9 the value is unchanged 201406.

Consequently, you'll have to wait a little bit for the standard value to come out.

--- Update ---

According to the C++ standard §19.8/p1 Predefined macro names [cpp.predefined] (Emphasis Mine):

1 The following macro names shall be defined by the implementation:

__cplusplus The integer literal 201703L.

Thus, the value of __cplusplus when using C++17 shall be 201703L.

like image 59
101010 Avatar answered Sep 21 '22 13:09

101010


I would try:

#if __cplusplus > 201402L   // C++14 code here   ... #endif 

In other words, testing for greater than C++14 should work as compilers add more features. As someone mentioned above, GCC uses 201500L. It looks like clang uses 201406L (four months after C++14 I guess).

Using the above snippet should be cross-platform and will work even when C++17 comes out with real value for __cplusplus. For more details about evolving features try the feature test macros.

like image 27
emsr Avatar answered Sep 25 '22 13:09

emsr