skip to Main Content

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


  1. On version 10 and 11 of GCC stdcpp Built-in macro __cplusplus defined like this

    GCC 10:

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

    GCC 11:

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

    On GCC 10.5.0 manual for C++ Standard Predefined Macros,

    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, or an unspecified value strictly larger than 201703L for the experimental languages enabled by -std=c++2a and -std=gnu++2a.

    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.

    Login or Signup to reply.
  2. This is not the correct way to check a feature-test macro. You don’t compare the value of __cplusplus to a given macro:

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

    Instead, you simply check the value of the macro itself:

    #if __cpp_lib_three_way_comparison >= 201917
    

    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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search