简体   繁体   English

c++14 中是否有 __cplusplus 的标准定义?

[英]Is there a standard definition for __cplusplus in c++14?

I'm looking to setup some preprocessor stuff, and I'd like a more exact number for what __cplusplus in C++14 should be defined as.我正在寻找设置一些预处理器的东西,我想要一个更准确的数字来说明 C++14 中的__cplusplus应该被定义为什么。 Is there one mandated by the standard?标准中是否有强制要求?

N3936 * §16.8 [cpp.predefined]/p1: N3936 * §16.8 [cpp.predefined]/p1:

1 The following macro names shall be defined by the implementation: 1 以下宏名称应由实现定义:

__cplusplus

The name __cplusplus is defined to the value 201402L when compiling a C++ translation unit.在编译 C++ 翻译单元时,名称__cplusplus被定义为值201402L

N3936 is the final working draft that became C++14, and the number 201402L is consistent with the meeting at which the C++14 standard is sent out for final balloting ( February 2014 ). N3936 是成为 C++14 的最终工作草案,编号201402L与 C++14 标准发出进行最终投票的会议( 2014 年 2 月)一致。

* Those interested in obtaining a copy of the C++ standard should check out Where do I find the current C or C++ standard documents? * 有兴趣获得 C++ 标准副本的人应该查看我在哪里可以找到当前的 C 或 C++ 标准文档?

cppreference has information on the standard values of the __cplusplus macro in the section " Predefined macros ." cppreference 在“ 预定义宏”部分中提供了有关__cplusplus宏的标准值的信息。 Currently the standard values are:目前的标准值为:

199711L (C++98 or C++03) 199711L (C++98 或 C++03)
201103L (C++11) 201103L (C++11)
201402L (C++14) 201402L (C++14)
201703L (C++17) 201703L (C++17)
202002L (C++20) 202002L (C++20)

The macro's value for any given version isn't firmly established until the final standard is published.在最终标准发布之前,任何给定版本的宏的价值都不会牢固确立。 Therefore, as of June 2019, there was no way to know what the macro value for C++2a would be (and as of Feb 2021 there's no way to know what the value will be for C++2b).因此,截至 2019 年 6 月,无法知道 C++2a 的宏值是多少(截至 2021 年 2 月,无法知道 C++2b 的值是多少)。

Library vendors typically gate their "C++2a" features on #if __cplusplus > 201703L , and their "C++2b" features on __cplusplus > 202002L , and so on.库供应商通常栅极他们的“C ++ 2A”功能上#if __cplusplus > 201703L ,并在他们的“C ++ 2B”设有__cplusplus > 202002L ,等等。

Compiler vendors with a "C++2a" mode simply picked any arbitrary value for __cplusplus that made the library vendors' checks happy:具有“C++2a”模式的编译器供应商只需为__cplusplus选择任意值,使库供应商的检查满意:

GCC (8.x thru 10.x) -std=c++2a mode uses __cplusplus == 201709L . GCC (8.x 到 10.x) -std=c++2a模式使用__cplusplus == 201709L
Clang (5.x thru 9.x) -std=c++2a mode uses __cplusplus == 201707L . Clang (5.x 到 9.x) -std=c++2a模式使用__cplusplus == 201707L
Microsoft Visual Studio (19.20 thru 19.28) /std:c++latest mode uses __cplusplus == 201705L if and only if you pass /Zc:__cplusplus ! Microsoft Visual Studio(19.20 至 19.28) /std:c++latest模式使用__cplusplus == 201705L 当且仅当您通过/Zc:__cplusplus Otherwise it uses 199711L .否则它使用199711L So watch out for that!所以要注意这一点!

How have transitions historically been handled?:历史上如何处理转换?:

Clang 4.0.1 -std=c++1z set __cplusplus == 201406L . Clang 4.0.1 -std=c++1z设置__cplusplus == 201406L Clang 5.0.0 introduced -std=c++17 and -std=c++2a , made -std=c++1z a synonym for -std=c++17 , and bumped the macro (no matter which of 17 / 1z you used) to the standard value 201703L . Clang 5.0.0 引入了-std=c++17-std=c++2a ,使-std=c++1z成为-std=c++17的同义词,并撞上了宏(无论17 / 1z您使用)到标准值201703L Clang 10.0 introduced -std=c++20 , made -std=c++2a a synonym for -std=c++20 , and bumped the macro to the standard value 202002L . Clang 10.0 引入了-std=c++20 ,使-std=c++2a成为-std=c++20的同义词,并将宏提升到标准值202002L As of Feb 2021, Clang has no formal "C++2b" mode.截至 2021 年 2 月,Clang 没有正式的“C++2b”模式。

GCC 5.1 introduced -std=c++1z and -std=c++17 as synonyms out of the gate, setting __cplusplus == 201500L . GCC 5.1 引入了-std=c++1z-std=c++17作为同义词,设置__cplusplus == 201500L GCC 7.1 bumped the value (no matter which spelling you used) to the standard value of 201703L . GCC 7.1 将该值(无论您使用哪种拼写)提高到201703L的标准值。 GCC 8.1 introduced -std=c++2a with __cplusplus == 201709L . GCC 8.1 引入了-std=c++2a__cplusplus == 201709L GCC 10.1 introduced -std=c++20 as a synonym for -std=c++2a (but left the macro at 201709L ). GCC 10.1 引入-std=c++20作为-std=c++2a的同义词(但将宏保留在201709L )。 As of Feb 2021, GCC trunk has introduced -std=c++2b with __cplusplus == 202100L .截至 2021 年 2 月,GCC 主干引入了-std=c++2b__cplusplus == 202100L

Oddly, according to Godbolt Compiler Explorer, MSVC bumped the macro for -std:c++latest mode from 201704L to 201705L sometime between MSVC 19.16 and 19.20.奇怪的是,根据 Godbolt Compiler Explorer 的说法,MSVC 在 MSVC 19.16 和 19.20 之间的201704L-std:c++latest模式的宏从201704L201705L As of Feb 2021, as far as I know, MSVC has no formal "C++20" mode.截至 2021 年 2 月,据我所知,MSVC 没有正式的“C++20”模式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM