Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

consider using __cpp_exceptions and/or __EXCEPTIONS to disable/enable exception support #498

Closed
user1095108 opened this issue Mar 11, 2017 · 21 comments

Comments

@user1095108
Copy link

user1095108 commented Mar 11, 2017

It is much more user-friendly if exception support is automatically detected.

resources:
https://gcc.gnu.org/onlinedocs/libstdc++/manual/using_exceptions.html
http://en.cppreference.com/w/cpp/experimental/feature_test

@nlohmann
Copy link
Owner

Thanks, I did not find these macros. Do you know which compilers are covered with them?

@user1095108
Copy link
Author

gcc and clang definitely, others I don't know. You can test for a specific compiler than go on from there. But it is a proposed standard extension too.

@user1095108
Copy link
Author

user1095108 commented Mar 11, 2017

I guess you could also do #if defined(__cpp_exceptions) && (1 == __cpp_exceptions)

No need to a specific compiler test then.

@nlohmann
Copy link
Owner

I'm checking for __EXCEPTIONS right now. No idea where I got this from...

@user1095108
Copy link
Author

user1095108 commented Mar 11, 2017

If you do it doesn't work too well :) I tried to compile without exception support and it didn't work, I checked and saw I'd have to define some macro manually to get your library to compile.

@nlohmann nlohmann self-assigned this Mar 11, 2017
@nlohmann nlohmann added this to the Release 3.0.0 milestone Mar 11, 2017
nlohmann added a commit that referenced this issue Mar 11, 2017
I used __EXCEPTIONS to detect whether exceptions are supported.
Apparently, this is a macro that is only used by libstdc++
(https://gcc.gnu.org/bugzilla/show_bug.cgi?id=64276). It’s much cleaner
to use __cpp_exceptions as it is in the standard since C++98.

Note that compiling the unit-tests with “-fno-exceptions” still does
not work, because Catch uses throw internally. However, the library’s
exceptions can be switched off by defining JSON_NOEXCEPTION.
@nlohmann nlohmann added the solution: proposed fix a fix for the issue has been proposed and waits for confirmation label Mar 11, 2017
@nlohmann
Copy link
Owner

@user1095108 I added a fix (see 65dfc97). Does this work for you?

@nlohmann
Copy link
Owner

Sigh... MSVC seems not to use __cpp_exceptions (Appveyor fails: https://ci.appveyor.com/project/nlohmann/json/build/1770). I read about _HAS_EXCEPTIONS so far, but did not find a reliable source for that.

@user1095108
Copy link
Author

I'd still check if __cpp_exceptions is defined. The macro is not part of c++98, just the exceptions themselves. Otherwise, great.

@nlohmann
Copy link
Owner

I'd still check if __cpp_exceptions is defined.

What do you mean?

@nlohmann
Copy link
Owner

From https://msdn.microsoft.com/en-us/library/b0084kay.aspx:

_CPPUNWIND Defined as 1 if one or more of the /GX (Enable Exception Handling), /clr (Common Language Runtime Compilation), or /EH (Exception Handling Model) compiler options are set. Otherwise, undefined.

@user1095108
Copy link
Author

It is a proposed extension of c++, gcc and clang already implement it, but MSVC has its own thing as you noticed.

@nlohmann
Copy link
Owner

I see. If the fix works for you, I can close the ticket.

@user1095108
Copy link
Author

user1095108 commented Mar 11, 2017 via email

@nlohmann
Copy link
Owner

I will, but first I wanted your OK, and I want to wait for Travis to finish.

@user1095108
Copy link
Author

user1095108 commented Mar 11, 2017 via email

@nlohmann
Copy link
Owner

You're right, it is better to used defined(). But I would not care about the value of the macro - if someone defines it, it better has the standard semantics.

@user1095108
Copy link
Author

user1095108 commented Mar 11, 2017 via email

@nlohmann
Copy link
Owner

$ g++ -x c++ -dM -E - < /dev/null | grep _cpp_exceptions

#define __cpp_exceptions 199711

@user1095108
Copy link
Author

user1095108 commented Mar 11, 2017 via email

@nlohmann
Copy link
Owner

GCC 4.9 does not define __cpp_exceptions. I might need __EXCEPTIONS there.

@nlohmann nlohmann removed the solution: proposed fix a fix for the issue has been proposed and waits for confirmation label Mar 11, 2017
@user1095108
Copy link
Author

user1095108 commented Mar 11, 2017 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants