From 6ea011f2b6e29f9efa3adbe2f06610041ceaca09 Mon Sep 17 00:00:00 2001 From: Florian Albrechtskirchinger Date: Tue, 12 Apr 2022 07:34:16 +0200 Subject: [PATCH] Exclude std::any from implicit conversion Fixes #3428 (MSVC) and silences compiler warning on GCC (-Wconversion). --- 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 db85f8d7c..609905a46 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 < std::is_same>, #endif detail::is_detected_lazy >::value, int >::type = 0 > diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index c1f545b07..71b08310a 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 < std::is_same>, #endif detail::is_detected_lazy >::value, int >::type = 0 > diff --git a/test/src/unit-regression2.cpp b/test/src/unit-regression2.cpp index 55bbcac65..feb800e76 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