nemequ / hedley

A C/C++ header to help move #ifdefs out of your code
https://nemequ.github.io/hedley/
Creative Commons Zero v1.0 Universal
774 stars 51 forks source link

add specific constexpr versions #62

Closed ZXShady closed 2 months ago

ZXShady commented 4 months ago

for example HEDLEY_CONSTEXPR14 for C++14 compiles into constexpr and above otherwise compile to no keyword HEDLEY_CONSTEXPR17 HEDLEY_CONSTEXPR20 .. etc... we have HEDLEY_CONSTEXPR but it is only limited for C++11

nemequ commented 2 months ago

This strikes me as a very slippery slope… I get that in C++ versions newer than C++11 you can do more stuff in a constexpr, but that's true of many keywords and I don't think having a bunch of different versions of each macro is a sustainable solution.

Besides, I don't think macros should embed the version number like that; the rest of Hedley is structured in a way where the API basically requests a feature, and Hedley does its best to figure out whether that feature is supported. For example, I see that you're not supposed to use constexpr on a virtual function before C++20, but what if a compiler implements the functionality and just allows it on a C++ versions. In that case, the test should really be for the compiler version, not the C++ version.

A more Hedley-esque version would be to have something like (untested, would definitely need more checks)

#if __cplusplus >= 202002L
#  define HEDLEY_CONSTEXPR_VIRTUAL constexpr virtual
#else
#  define HEDLEY_CONSTEXPR_VIRTUAL virtual
#endif

Which is something I'd consider on a case-by-case basis.