Allow to use get with explicit constructor (#3079)

*  remove "fix" that caused #3077
pull/3082/head
Niels Lohmann 2021-10-14 21:01:14 +02:00 committed by GitHub
parent 0e694b4060
commit ba046e4ea4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 36 additions and 2 deletions

View File

@ -3069,7 +3069,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
ValueType get_impl(detail::priority_tag<0> /*unused*/) const noexcept(noexcept(
JSONSerializer<ValueType>::from_json(std::declval<const basic_json_t&>(), std::declval<ValueType&>())))
{
ValueType ret{};
auto ret = ValueType();
JSONSerializer<ValueType>::from_json(*this, ret);
return ret;
}

View File

@ -20569,7 +20569,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
ValueType get_impl(detail::priority_tag<0> /*unused*/) const noexcept(noexcept(
JSONSerializer<ValueType>::from_json(std::declval<const basic_json_t&>(), std::declval<ValueType&>())))
{
ValueType ret{};
auto ret = ValueType();
JSONSerializer<ValueType>::from_json(*this, ret);
return ret;
}

View File

@ -201,6 +201,32 @@ template<class T>
class my_allocator : public std::allocator<T>
{};
/////////////////////////////////////////////////////////////////////
// for #3077
/////////////////////////////////////////////////////////////////////
class FooAlloc
{};
class Foo
{
public:
explicit Foo(const FooAlloc& /* unused */ = FooAlloc()) {}
bool value = false;
};
class FooBar
{
public:
Foo foo{};
};
inline void from_json(const nlohmann::json& j, FooBar& fb)
{
j.at("value").get_to(fb.foo.value);
}
TEST_CASE("regression tests 2")
{
SECTION("issue #1001 - Fix memory leak during parser callback")
@ -712,6 +738,14 @@ TEST_CASE("regression tests 2")
CHECK(j_path == text_path);
}
#endif
SECTION("issue #3077 - explicit constructor with default does not compile")
{
json j;
j[0]["value"] = true;
std::vector<FooBar> foo;
j.get_to(foo);
}
}
DOCTEST_CLANG_SUPPRESS_WARNING_POP