How to determine if my code compiled with -fno-exceptions?

I am writing a C ++ library, and I would like to make my APIs exceptions for invalid parameters, but rely on statements instead when the code is compiled with -fno-exceptions .

Is there any way to detect at compile time if I am allowed to use exception handling? Please note that I write the library for headers only, so I donโ€™t have the configure phase and I donโ€™t have access to the build system to just define a macro on the command line (and I donโ€™t want to add a burden to the user).

Since the Standard does not have the concept of "-fno-exceptions", of course, the solution may be compiler dependent. In this case, I'm interested in solutions that work with both g ++ and clang ++, other compilers are not important for this project.

Thank you very much

+6
source share
1 answer

GCC and Clang define the __EXCEPTIONS macro if exceptions are enabled, and do not define it if exceptions are disabled with -fno-exceptions .

Example:

 #include <cstdio> int main() { #ifdef __EXCEPTIONS puts("Exceptions are enabled"); #else puts("Exceptions are disabled"); #endif } 
+13
source

Source: https://habr.com/ru/post/973698/


All Articles