在 G++ 上检查 C++20 功能的正确方法是什么

问题描述 投票:0回答:1

我的理解最好的做法是:

#if __cplusplus >= 202002L
// code
#endif

但是,即使我使用

-std=c++20
进行编译,它也不起作用。

另外,

g++ -x c++ -std=c++20 -dM -E - </dev/null | grep __cplusplus
的输出是:

#define __cplusplus 201709L

这真的让我很困惑,因为我认为 C++17 的正确定义是 201703L。

我错过了什么吗?

我尝试检查是否存在

<=>
支持。所以我也可以尝试这样做:

#if __cplusplus >= __cpp_impl_three_way_comparison && __cplusplus >= __cpp_lib_three_way_comparison

但是,这不起作用,因为这两个常数都表示

201907L

附注

$ g++ --version
g++ (Debian 10.2.1-6) 10.2.1 20210110
Copyright (C) 2020 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
c++ g++ c++20
1个回答
0
投票

在 GCC stdcpp 版本 10 和 11 上内置宏

__cplusplus
定义如下

海湾合作委员会10:

      if (CPP_OPTION (pfile, lang) == CLK_CXX2A
      || CPP_OPTION (pfile, lang) == CLK_GNUCXX2A)
    _cpp_define_builtin (pfile, "__cplusplus 201709L");

海湾合作委员会11:

      else if (CPP_OPTION (pfile, lang) == CLK_CXX20
      || CPP_OPTION (pfile, lang) == CLK_GNUCXX20)
    _cpp_define_builtin (pfile, "__cplusplus 202002L");

关于 C++ 标准预定义宏的 GCC 10.5.0 手册,

根据所选的语言标准,宏的值为 199711L(对于 1998 C++ 标准)、201103L(对于 2011 C++ 标准)、201402L(对于 2014 C++ 标准)、201703L(对于 2017 C++ 标准)或严格大于 201703L 的未指定值对于 -std=c++2a 和 -std=gnu++2a 启用的实验语言。

由于 GCC 的最新版本设置了

__cplusplus
unspecified value strictly larger than 202302L for the experimental languages enabled by -std=c++26 and -std=gnu++26
,该任意值似乎是他们实验支持的工作约定。

© www.soinside.com 2019 - 2024. All rights reserved.