Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exclude std::any from implicit conversion (fixes #3428) #3437

Merged
merged 2 commits into from
Apr 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions include/nlohmann/json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ SOFTWARE.
#include <nlohmann/ordered_map.hpp>

#if defined(JSON_HAS_CPP_17)
#include <any>
#include <string_view>
#endif

Expand Down Expand Up @@ -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<std::is_same<ValueType, std::string_view>>,
#endif
#if defined(JSON_HAS_CPP_17)
detail::negation<std::is_same<ValueType, std::any>>,
#endif
detail::is_detected_lazy<detail::get_template_function, const basic_json_t&, ValueType>
>::value, int >::type = 0 >
Expand Down
4 changes: 4 additions & 0 deletions single_include/nlohmann/json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17287,6 +17287,7 @@ template <class Key, class T, class IgnoredLess = std::less<Key>,


#if defined(JSON_HAS_CPP_17)
#include <any>
#include <string_view>
#endif

Expand Down Expand Up @@ -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<std::is_same<ValueType, std::string_view>>,
#endif
#if defined(JSON_HAS_CPP_17)
detail::negation<std::is_same<ValueType, std::any>>,
#endif
detail::is_detected_lazy<detail::get_template_function, const basic_json_t&, ValueType>
>::value, int >::type = 0 >
Expand Down
13 changes: 13 additions & 0 deletions test/src/unit-regression2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ using ordered_json = nlohmann::ordered_json;
#include <utility>

#ifdef JSON_HAS_CPP_17
#include <any>
#include <variant>
#endif

Expand Down Expand Up @@ -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