0

Does gcc define a macro or something else, when I use the -std=c++zz flag? Imagine, that I have a (nice and easy and fast and...) solution in my program which only works with g++ -std=c++20. However I want to provide a (less better but working) solution which works with -std=c++11 or something else. For example:

#ifdef __STD20__
#include <numbers>
const double PI=std::numbers::pi;
#else
const double PI=4.0*atan(1);
#endif

Or should I handle it somehow in the makefile? Thank you for your advice.

(NOTE: the question is not about defining pi; it is just the shortest example I could provide.)

Tom Solid
  • 2,226
  • 1
  • 13
  • 32
  • I believe the marked duplicate answers your question, if it does not, please let me know. – Quimby Jul 26 '23 at 07:05
  • 1
    I've reopened this because "C++ version" is not the correct way of checking for modern features, which have associated test macros. – HolyBlackCat Jul 26 '23 at 07:08
  • In this case you can use `#ifdef __cpp_lib_math_constants` – Alan Birtles Jul 26 '23 at 07:08
  • ...preceded by `#if __has_include()` `#include `. – HolyBlackCat Jul 26 '23 at 07:09
  • OP, what feature your actual code uses? Surely not ``? – HolyBlackCat Jul 26 '23 at 07:10
  • I think you are better off testing individual features of C++ see [feature test](https://en.cppreference.com/w/cpp/feature_test). – Pepijn Kramer Jul 26 '23 at 07:44
  • Thank you for your answers. Yes, it have turned out, that my question is a duplicate. My original intention was answered here: https://stackoverflow.com/questions/2324658 (but I couldn't find it before post my question.) However, the individual `feature test` that have been suggested in your comments and during the second closure is a better solution. Inspired by this [meta anwser](https://meta.stackoverflow.com/a/297547/6113363) I wouldn't remove the question, to help the ones like me. Thank you again. – Tom Solid Jul 26 '23 at 08:08

0 Answers0