From 65dfc97d401e17befa4d9a2320660b381529304e Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Sat, 11 Mar 2017 17:44:54 +0100 Subject: [PATCH 1/4] :hammer: using __cpp_exceptions to detect exception support #498 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- src/json.hpp | 2 +- src/json.hpp.re2c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/json.hpp b/src/json.hpp index 33456a51..9fb5fefe 100644 --- a/src/json.hpp +++ b/src/json.hpp @@ -82,7 +82,7 @@ SOFTWARE. #endif // allow to disable exceptions -#if not defined(JSON_NOEXCEPTION) || defined(__EXCEPTIONS) +#if __cpp_exceptions && not defined(JSON_NOEXCEPTION) #define JSON_THROW(exception) throw exception #define JSON_TRY try #define JSON_CATCH(exception) catch(exception) diff --git a/src/json.hpp.re2c b/src/json.hpp.re2c index 180f5d3b..a837df06 100644 --- a/src/json.hpp.re2c +++ b/src/json.hpp.re2c @@ -82,7 +82,7 @@ SOFTWARE. #endif // allow to disable exceptions -#if not defined(JSON_NOEXCEPTION) || defined(__EXCEPTIONS) +#if __cpp_exceptions && not defined(JSON_NOEXCEPTION) #define JSON_THROW(exception) throw exception #define JSON_TRY try #define JSON_CATCH(exception) catch(exception) From e3e6cbecc7a150590636d13b17a0260b5c46d90e Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Sat, 11 Mar 2017 17:59:24 +0100 Subject: [PATCH 2/4] :checkered_flag: added check for _CPPUNWIND MSVC does not define __cpp_exceptions, but seems to use _CPPUNWIND when exception support is switched on, see https://msdn.microsoft.com/en-us/library/b0084kay.aspx. --- src/json.hpp | 2 +- src/json.hpp.re2c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/json.hpp b/src/json.hpp index 9fb5fefe..f9a92820 100644 --- a/src/json.hpp +++ b/src/json.hpp @@ -82,7 +82,7 @@ SOFTWARE. #endif // allow to disable exceptions -#if __cpp_exceptions && not defined(JSON_NOEXCEPTION) +#if (__cpp_exceptions || defined(_CPPUNWIND)) && not defined(JSON_NOEXCEPTION) #define JSON_THROW(exception) throw exception #define JSON_TRY try #define JSON_CATCH(exception) catch(exception) diff --git a/src/json.hpp.re2c b/src/json.hpp.re2c index a837df06..2cad18aa 100644 --- a/src/json.hpp.re2c +++ b/src/json.hpp.re2c @@ -82,7 +82,7 @@ SOFTWARE. #endif // allow to disable exceptions -#if __cpp_exceptions && not defined(JSON_NOEXCEPTION) +#if (__cpp_exceptions || defined(_CPPUNWIND)) && not defined(JSON_NOEXCEPTION) #define JSON_THROW(exception) throw exception #define JSON_TRY try #define JSON_CATCH(exception) catch(exception) From 122afbf1280413ade3719d6af2c37352215e30f0 Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Sat, 11 Mar 2017 18:43:21 +0100 Subject: [PATCH 3/4] :hammer: added defined() check --- src/json.hpp | 2 +- src/json.hpp.re2c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/json.hpp b/src/json.hpp index f9a92820..725310f6 100644 --- a/src/json.hpp +++ b/src/json.hpp @@ -82,7 +82,7 @@ SOFTWARE. #endif // allow to disable exceptions -#if (__cpp_exceptions || defined(_CPPUNWIND)) && not defined(JSON_NOEXCEPTION) +#if (defined(__cpp_exceptions) || defined(_CPPUNWIND)) && not defined(JSON_NOEXCEPTION) #define JSON_THROW(exception) throw exception #define JSON_TRY try #define JSON_CATCH(exception) catch(exception) diff --git a/src/json.hpp.re2c b/src/json.hpp.re2c index 2cad18aa..9172d2bf 100644 --- a/src/json.hpp.re2c +++ b/src/json.hpp.re2c @@ -82,7 +82,7 @@ SOFTWARE. #endif // allow to disable exceptions -#if (__cpp_exceptions || defined(_CPPUNWIND)) && not defined(JSON_NOEXCEPTION) +#if (defined(__cpp_exceptions) || defined(_CPPUNWIND)) && not defined(JSON_NOEXCEPTION) #define JSON_THROW(exception) throw exception #define JSON_TRY try #define JSON_CATCH(exception) catch(exception) From 4b9c2f128740842abb8ba60956778bb59af15bd4 Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Sat, 11 Mar 2017 20:16:13 +0100 Subject: [PATCH 4/4] :hammer: added __EXCEPTIONS to the list --- src/json.hpp | 2 +- src/json.hpp.re2c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/json.hpp b/src/json.hpp index 725310f6..c7b0154a 100644 --- a/src/json.hpp +++ b/src/json.hpp @@ -82,7 +82,7 @@ SOFTWARE. #endif // allow to disable exceptions -#if (defined(__cpp_exceptions) || defined(_CPPUNWIND)) && not defined(JSON_NOEXCEPTION) +#if (defined(__cpp_exceptions) || defined(__EXCEPTIONS) || defined(_CPPUNWIND)) && not defined(JSON_NOEXCEPTION) #define JSON_THROW(exception) throw exception #define JSON_TRY try #define JSON_CATCH(exception) catch(exception) diff --git a/src/json.hpp.re2c b/src/json.hpp.re2c index 9172d2bf..3ee67b1a 100644 --- a/src/json.hpp.re2c +++ b/src/json.hpp.re2c @@ -82,7 +82,7 @@ SOFTWARE. #endif // allow to disable exceptions -#if (defined(__cpp_exceptions) || defined(_CPPUNWIND)) && not defined(JSON_NOEXCEPTION) +#if (defined(__cpp_exceptions) || defined(__EXCEPTIONS) || defined(_CPPUNWIND)) && not defined(JSON_NOEXCEPTION) #define JSON_THROW(exception) throw exception #define JSON_TRY try #define JSON_CATCH(exception) catch(exception)