Skip to content

Commit 1deeb43

Browse files
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
1 parent 1034490 commit 1deeb43

File tree

3 files changed

+21
-0
lines changed

3 files changed

+21
-0
lines changed

include/nlohmann/json.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ SOFTWARE.
9494
#include <nlohmann/ordered_map.hpp>
9595

9696
#if defined(JSON_HAS_CPP_17)
97+
#include <any>
9798
#include <string_view>
9899
#endif
99100

@@ -1891,6 +1892,9 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
18911892

18921893
#if defined(JSON_HAS_CPP_17) && (defined(__GNUC__) || (defined(_MSC_VER) && _MSC_VER >= 1910 && _MSC_VER <= 1914))
18931894
detail::negation<std::is_same<ValueType, std::string_view>>,
1895+
#endif
1896+
#if defined(JSON_HAS_CPP_17)
1897+
detail::negation<std::is_same<ValueType, std::any>>,
18941898
#endif
18951899
detail::is_detected_lazy<detail::get_template_function, const basic_json_t&, ValueType>
18961900
>::value, int >::type = 0 >

single_include/nlohmann/json.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17287,6 +17287,7 @@ template <class Key, class T, class IgnoredLess = std::less<Key>,
1728717287

1728817288

1728917289
#if defined(JSON_HAS_CPP_17)
17290+
#include <any>
1729017291
#include <string_view>
1729117292
#endif
1729217293

@@ -19084,6 +19085,9 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
1908419085

1908519086
#if defined(JSON_HAS_CPP_17) && (defined(__GNUC__) || (defined(_MSC_VER) && _MSC_VER >= 1910 && _MSC_VER <= 1914))
1908619087
detail::negation<std::is_same<ValueType, std::string_view>>,
19088+
#endif
19089+
#if defined(JSON_HAS_CPP_17)
19090+
detail::negation<std::is_same<ValueType, std::any>>,
1908719091
#endif
1908819092
detail::is_detected_lazy<detail::get_template_function, const basic_json_t&, ValueType>
1908919093
>::value, int >::type = 0 >

test/src/unit-regression2.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ using ordered_json = nlohmann::ordered_json;
4343
#include <utility>
4444

4545
#ifdef JSON_HAS_CPP_17
46+
#include <any>
4647
#include <variant>
4748
#endif
4849

@@ -860,6 +861,18 @@ TEST_CASE("regression tests 2")
860861
CHECK(obj.name == "class");
861862
}
862863
#endif
864+
865+
#if defined(JSON_HAS_CPP_17) && JSON_USE_IMPLICIT_CONVERSIONS
866+
SECTION("issue #3428 - Error occurred when converting nlohmann::json to std::any")
867+
{
868+
json j;
869+
std::any a1 = j;
870+
std::any&& a2 = j;
871+
872+
CHECK(a1.type() == typeid(j));
873+
CHECK(a2.type() == typeid(j));
874+
}
875+
#endif
863876
}
864877

865878
DOCTEST_CLANG_SUPPRESS_WARNING_POP

0 commit comments

Comments
 (0)