My understanding was the best practice is:
#if __cplusplus >= 202002L
// code
#endif
However, it does not work, even though I do compile with -std=c++20
.
Also, the output of g++ -x c++ -std=c++20 -dM -E - </dev/null | grep __cplusplus
is:
#define __cplusplus 201709L
Which really confuses me, because I assumed that the correct define for C++17 is 201703L.
Am i missing something?
I have tried to check if <=>
support exists. So i can also try to do:
#if __cplusplus >= __cpp_impl_three_way_comparison && __cplusplus >= __cpp_lib_three_way_comparison
However, that does not work, as both of those constants say 201907L
.
P.S.
$ 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.
2
Answers
On version 10 and 11 of GCC stdcpp Built-in macro
__cplusplus
defined like thisGCC 10:
GCC 11:
On GCC 10.5.0 manual for C++ Standard Predefined Macros,
Due to the fact that the recent version of GCC sets
__cplusplus
unspecified value strictly larger than 202302L for the experimental languages enabled by -std=c++26 and -std=gnu++26
, that arbitrary value seems to be their working convention for experimental support.This is not the correct way to check a feature-test macro. You don’t compare the value of
__cplusplus
to a given macro:Instead, you simply check the value of the macro itself:
The value to check against comes from the feature-test macro list.
Which as you can see, gcc 10.2 does define to 201907 (if you include the appropriate header so that you can do the check).
In general, there’s not much reason to check
__cplusplus
directly. It’s just not granular enough – it can only really give you one possible value, and that can never tell you all the information you need to know. That’s why the feature-test macro exist: so we have one macro per feature, and those can be checked independently. They also, for the most part †, allow the implementations to adopt features in arbitrary order – and have that be reflected faithfully in the macros.† Sometimes multiple proposals bump a specific feature-test macro at the same time. This either necessitates the implementation ship all of those proposals at the same time, ship some but not advertise those, or in some cases we just artificially increment the macro to make this a little easier on implementations – e.g.
__cpp_lib_format
.