__cplusplus 对于 C++17 的值是多少?

2022-01-11 00:00:00 macros c++ c++17

我们正在尝试在 C++17 及其 更改为 std::uncaught_exception 下测试一些代码.我似乎无法让 GCC 提供 __cplusplus 的值:

$/opt/local/bin/g++ -std=c++17 -dM -E - </dev/null |grep __cpluspluscc1:警告:命令行选项-std=c++1z"对 C++/ObjC++ 有效,但对 C 无效$

还有:

$/opt/local/bin/g++ --versiong++-mp-6 (MacPorts gcc6 6.1.0_0) 6.1.0版权所有 (C) 2016 Free Software Foundation, Inc.

使用C++17时__cplusplus的值是多少?

解决方案

tl;dr: 对于 C++17,__cplusplus201703L.

<块引用>

使用C++17时__cplusplus的值是多少?

根据标准草案N4594§16.8/p1 预定义的宏名称 [cpp.predefined](Emphasis Mine):

<块引用>

以下宏名称应由实现定义:__cplusplus 名称 __cplusplus 定义为值201402L 编译 C++ 翻译单元时.156

156) 本标准的未来版本旨在用更大的值替换这个宏的值.不合格编译器应该使用最多五个十进制数字的值.

但是,为 C++14 标准指定了相同的值.显然,目前还没有为 C++17 标准设置官方/标准 __cplusplus 值.

在 GCC 版本 6.1 和 7.0 中,该值更改为 201500

现场演示

在 Clang 版本 3.8 和 3.9 中,该值保持不变201406.

因此,您必须稍等片刻才能得出标准值.

--- 更新---

根据 C++ 标准§19.8/p1 预定义的宏名称 [cpp.predefined](Emphasis Mine):

<块引用>

1 以下宏名称应由实现:

__cplusplus 整数字面量201703L.

因此,使用C++17时__cplusplus的值为201703L.

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?

解决方案

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.

相关文章