C++中__cplusplus宏介绍

本文介绍了C++编程语言中的预定义宏__cplusplus,它标识了所使用的C++标准版本。通过查看GCC编译器的预定义宏,我们可以发现__cplusplus的值对应不同的C++标准,例如201402L代表C++14标准。文章还探讨了如何通过命令行检查宏的定义,并对宏的来源和定义位置进行了讨论。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

背景

GCC中实现的C++标准库__cplusplus是一个很常见的宏。

弄清这个宏之前,我们需要先明白编程语言和编译器之间的关系。C++是一门编程语言,它有其标准。而编译C++代码的编译器有多种,如常见的Gnu Compiler CollectionClangVisual C++ 2017 Community。不同的C++编译器需要遵守/支持C++的标准。

了解上述背景之后,我们来查看__cplusplus宏在C++标准的要求和编译器中的定义。


C++标准和编译器中的预定义宏

鉴于C++的标准文档需要收费,我们在cppreference中查看关于__cplusplus的标准信息。

# 标准规定支持该宏
__cplusplus
denotes the version of C++ standard that is being used, expands to value 199711L(until C++11), 201103L(C++11), 201402L(C++14), 201703L(C++17), or 202002L(C++20)
(macro constant)

我使用的是Ubuntu系统,所以我们查看下GCC标准预定义宏。关于windows系统中__cplusplus宏定义的查看,可参考/Zc:__cplusplus(启用更新的 __cplusplus 宏

# GCC遵守C++标准支持该宏
__cplusplus
This macro is defined when the C++ compiler is in use. You can use __cplusplus to test whether a header is compiled by a C compiler or a C++ compiler. This macro is similar to __STDC_VERSION__, in that it expands to a version number. Depending on the language standard selected, the value of the macro is 199711L for the 1998 C++ standard, 201103L for the 2011 C++ standard, 201402L for the 2014 C++ standard, 201703L for the 2017 C++ standard, 202002L for the 2020 C++ standard, or an unspecified value strictly larger than 202002L for the experimental languages enabled by -std=c++23 and -std=gnu++23.

这里,我们知道__cplusplus宏的含义是“C++的版本“。接下来我们查看当前系统该宏的值是多少。


查看__cplusplus宏定义的值

参考Which C++ standard is the default when compiling with g++?,我们使用下面命令查看宏的值。

# 输出编译器预定义的宏,grep进行过滤
➜  g++ -dM -E -x c++  /dev/null | grep -F __cplusplus
#define __cplusplus 201402L

关于GCC的命令行参数,可通过GCC手册查看。上面的参数含义如下:

-dM -E # 输出预定义的宏
Instead of the normal output, generate a list of ‘#define’ directives
for all the macros defined during the execution of the preprocessor,
including predefined macros. This gives you a way of finding out
what is predefined in your version of the preprocessor. 
If you use ‘-dM’ without the ‘-E’ option, ‘-dM’ is interpreted as a
synonym for ‘-fdump-rtl-mach’

-E # 预处理之后停止,预处理的结果输出到标准输出
Stop after the preprocessing stage; do not run the compiler proper. The output
is in the form of preprocessed source code, which is sent to the standard output.
Input files that don’t require preprocessing are ignored

-x language # 指定语言
Specify explicitly the language

我们已经知道该宏的值,那这个宏在哪里/何时定义的呢?


__cplusplus宏定义的位置

结论:我暂时不知道,如果你知道的话,可以评论区留言

我知道c++config用于预定义C++库的宏。但是编译器预定义的宏的位置在哪呢?(或者说,这些宏本身的创建过程是什么样子的?)

我下载了GCC源码,它使用autotools进行构建。我企图从这里找到答案。我使用关键字在源代码中进行搜素,但是并没有找到答案。


参考

Where do I find the current C or C++ standard documents?

What does the “__cplusplus” macro expand to?

Which C++ standard is the default when compiling with g++?

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

da1234cao

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值