2

There are a lot of feature test macros in C++ which give a simple and portable way to detect the presence of C++ standards and experimental features. However, I didn't manage to find simple macros to check if enum classes are supported. Is there more simple and straightforward way to check enum class support then checking the value of __cplusplus macros?

pablo285
  • 2,460
  • 4
  • 14
  • 38
Seleznev Anton
  • 641
  • 5
  • 14
  • 1
    Uhh.... Why not just use them and let the compiler generate the errors if it isn't supported? If you want to support platforms that don't have this feature, then you're going to write a fallback anyway. Why not use the same code for those that support it as well? – Ivan Rubinson Jul 26 '18 at 19:50
  • I want to write code that will compile on different compilers. And for those that don't support them use simple enums. – Seleznev Anton Jul 26 '18 at 19:51
  • Then don't use features that certain compilers don't support. – Ivan Rubinson Jul 26 '18 at 19:51
  • 3
    enum class can be simply remplace with a class and an enum why write a check and a fallback if the fallback always work – Tyker Jul 26 '18 at 19:57
  • Well that't true. Actually didn't think about simple class and enum inside. Maybe it will suit me. Thanks for the hint. I'll consider it. – Seleznev Anton Jul 26 '18 at 19:59
  • 1
    @Tyker: Fallback doesn't provide type safety – JVApen Jul 26 '18 at 20:05
  • Couldn't you just check if using C++11? – Moia Jul 30 '18 at 11:33
  • 1
    This is normally done at configuration time. Both cmake and gnu autoconf support this. You set up a macro based on whether a small test program succeeds or fails to run. – n. m. could be an AI Jul 30 '18 at 12:09

1 Answers1

1

Unfortunately there is no portable way to detect this.

Boost has a macro called BOOST_NO_CXX11_SCOPED_ENUMS so if you are using Boost in your projects you can use the value. However they are setting it on compiler/version basis - there is no clever coding trick behind this.

If Boost is not an option for you then you can at least have a look at how they are doing it and copy parts of their code for your purposes if you are lucky enough to use compilers which they list here:

Boost compiler configurations

pablo285
  • 2,460
  • 4
  • 14
  • 38