From b8012876a5988ba6066aa1c46e33b2ecf5f14f86 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9o=20DELRIEU?= Date: Sat, 14 Jan 2017 02:20:53 +0100 Subject: [PATCH] add noexcept checks, and some missing noexcepts --- src/json.hpp | 5 +++-- test/CMakeLists.txt | 1 + test/src/unit-noexcept.cpp | 27 +++++++++++++++++++++++++++ 3 files changed, 31 insertions(+), 2 deletions(-) create mode 100644 test/src/unit-noexcept.cpp diff --git a/src/json.hpp b/src/json.hpp index 96221664..4b8f9e5a 100644 --- a/src/json.hpp +++ b/src/json.hpp @@ -843,9 +843,10 @@ struct adl_serializer } template - static void to_json(Json& j, T&& val) + static void to_json(Json &j, T &&val) noexcept( + noexcept(::nlohmann::to_json(j, std::forward(val)))) { - ::nlohmann::to_json(j, std::forward(val)); + ::nlohmann::to_json(j, std::forward(val)); } }; diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index a988f47b..0ceb6bf6 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -29,6 +29,7 @@ add_executable(${JSON_UNITTEST_TARGET_NAME} "src/unit-meta.cpp" "src/unit-modifiers.cpp" "src/unit-msgpack.cpp" + "src/unit-noexcept.cpp" "src/unit-pointer_access.cpp" "src/unit-readme.cpp" "src/unit-reference_access.cpp" diff --git a/test/src/unit-noexcept.cpp b/test/src/unit-noexcept.cpp new file mode 100644 index 00000000..212abdf7 --- /dev/null +++ b/test/src/unit-noexcept.cpp @@ -0,0 +1,27 @@ +#include "catch.hpp" + +#include "json.hpp" + +using nlohmann::json; + +enum test +{ +}; + +struct pod {}; +struct pod_bis {}; + +void to_json(json &, pod) noexcept; +void to_json(json &, pod_bis); +static json j; + +static_assert(noexcept(json{}), ""); +static_assert(noexcept(nlohmann::to_json(j, 2)), ""); +static_assert(noexcept(nlohmann::to_json(j, 2.5)), ""); +static_assert(noexcept(nlohmann::to_json(j, true)), ""); +static_assert(noexcept(nlohmann::to_json(j, test{})), ""); +static_assert(noexcept(nlohmann::to_json(j, pod{})), ""); +static_assert(not noexcept(nlohmann::to_json(j, pod_bis{})), ""); +static_assert(noexcept(json(2)), ""); +static_assert(noexcept(json(test{})), ""); +static_assert(noexcept(json(pod{})), "");