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{})), "");