Skip to content

Commit ba046e4

Browse files
authored
Allow to use get with explicit constructor (#3079)
* ⏪ remove "fix" that caused #3077
1 parent 0e694b4 commit ba046e4

File tree

3 files changed

+36
-2
lines changed

3 files changed

+36
-2
lines changed

include/nlohmann/json.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3069,7 +3069,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
30693069
ValueType get_impl(detail::priority_tag<0> /*unused*/) const noexcept(noexcept(
30703070
JSONSerializer<ValueType>::from_json(std::declval<const basic_json_t&>(), std::declval<ValueType&>())))
30713071
{
3072-
ValueType ret{};
3072+
auto ret = ValueType();
30733073
JSONSerializer<ValueType>::from_json(*this, ret);
30743074
return ret;
30753075
}

single_include/nlohmann/json.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20569,7 +20569,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
2056920569
ValueType get_impl(detail::priority_tag<0> /*unused*/) const noexcept(noexcept(
2057020570
JSONSerializer<ValueType>::from_json(std::declval<const basic_json_t&>(), std::declval<ValueType&>())))
2057120571
{
20572-
ValueType ret{};
20572+
auto ret = ValueType();
2057320573
JSONSerializer<ValueType>::from_json(*this, ret);
2057420574
return ret;
2057520575
}

test/src/unit-regression2.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,32 @@ template<class T>
201201
class my_allocator : public std::allocator<T>
202202
{};
203203

204+
/////////////////////////////////////////////////////////////////////
205+
// for #3077
206+
/////////////////////////////////////////////////////////////////////
207+
208+
class FooAlloc
209+
{};
210+
211+
class Foo
212+
{
213+
public:
214+
explicit Foo(const FooAlloc& /* unused */ = FooAlloc()) {}
215+
216+
bool value = false;
217+
};
218+
219+
class FooBar
220+
{
221+
public:
222+
Foo foo{};
223+
};
224+
225+
inline void from_json(const nlohmann::json& j, FooBar& fb)
226+
{
227+
j.at("value").get_to(fb.foo.value);
228+
}
229+
204230
TEST_CASE("regression tests 2")
205231
{
206232
SECTION("issue #1001 - Fix memory leak during parser callback")
@@ -712,6 +738,14 @@ TEST_CASE("regression tests 2")
712738
CHECK(j_path == text_path);
713739
}
714740
#endif
741+
742+
SECTION("issue #3077 - explicit constructor with default does not compile")
743+
{
744+
json j;
745+
j[0]["value"] = true;
746+
std::vector<FooBar> foo;
747+
j.get_to(foo);
748+
}
715749
}
716750

717751
DOCTEST_CLANG_SUPPRESS_WARNING_POP

0 commit comments

Comments
 (0)