From 1deeb434c679e7908875ce4ce0ce4583071ecc74 Mon Sep 17 00:00:00 2001 From: Florian Albrechtskirchinger Date: Tue, 12 Apr 2022 14:08:20 +0200 Subject: [PATCH] Exclude std::any from implicit conversion (fixes #3428) (#3437) * Exclude std::any from implicit conversion Fixes #3428 (MSVC) and silences compiler warning on GCC (-Wconversion). * Exclude std::any from implicit conversion --- include/nlohmann/json.hpp | 4 ++++ single_include/nlohmann/json.hpp | 4 ++++ test/src/unit-regression2.cpp | 13 +++++++++++++ 3 files changed, 21 insertions(+) diff --git a/include/nlohmann/json.hpp b/include/nlohmann/json.hpp index db85f8d7c1..b776708031 100644 --- a/include/nlohmann/json.hpp +++ b/include/nlohmann/json.hpp @@ -94,6 +94,7 @@ SOFTWARE. #include #if defined(JSON_HAS_CPP_17) + #include #include #endif @@ -1891,6 +1892,9 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec #if defined(JSON_HAS_CPP_17) && (defined(__GNUC__) || (defined(_MSC_VER) && _MSC_VER >= 1910 && _MSC_VER <= 1914)) detail::negation>, +#endif +#if defined(JSON_HAS_CPP_17) + detail::negation>, #endif detail::is_detected_lazy >::value, int >::type = 0 > diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index c1f545b071..53c15b651e 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -17287,6 +17287,7 @@ template , #if defined(JSON_HAS_CPP_17) + #include #include #endif @@ -19084,6 +19085,9 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec #if defined(JSON_HAS_CPP_17) && (defined(__GNUC__) || (defined(_MSC_VER) && _MSC_VER >= 1910 && _MSC_VER <= 1914)) detail::negation>, +#endif +#if defined(JSON_HAS_CPP_17) + detail::negation>, #endif detail::is_detected_lazy >::value, int >::type = 0 > diff --git a/test/src/unit-regression2.cpp b/test/src/unit-regression2.cpp index 55bbcac654..feb800e76e 100644 --- a/test/src/unit-regression2.cpp +++ b/test/src/unit-regression2.cpp @@ -43,6 +43,7 @@ using ordered_json = nlohmann::ordered_json; #include #ifdef JSON_HAS_CPP_17 + #include #include #endif @@ -860,6 +861,18 @@ TEST_CASE("regression tests 2") CHECK(obj.name == "class"); } #endif + +#if defined(JSON_HAS_CPP_17) && JSON_USE_IMPLICIT_CONVERSIONS + SECTION("issue #3428 - Error occurred when converting nlohmann::json to std::any") + { + json j; + std::any a1 = j; + std::any&& a2 = j; + + CHECK(a1.type() == typeid(j)); + CHECK(a2.type() == typeid(j)); + } +#endif } DOCTEST_CLANG_SUPPRESS_WARNING_POP