diff --git a/include/nlohmann/detail/conversions/from_json.hpp b/include/nlohmann/detail/conversions/from_json.hpp index b29ac05c6..aaf75e218 100644 --- a/include/nlohmann/detail/conversions/from_json.hpp +++ b/include/nlohmann/detail/conversions/from_json.hpp @@ -249,17 +249,23 @@ void()) from_json_array_impl(j, arr, priority_tag<3> {}); } -template < typename BasicJsonType, typename Array, std::size_t... Is > -Array from_json_array_impl(BasicJsonType&& j, identity_tag /*unused*/, index_sequence /*unused*/) +template < typename T, typename BasicJsonType, typename ArrayType, std::size_t... Idx> +ArrayType from_json_inplace_array_impl_base(BasicJsonType&& j, identity_tag /*unused*/, + index_sequence /*unused*/) { - return { std::forward(j).at(Is).template get()... }; + return { std::forward(j).at(Idx).template get()... }; } -template < typename BasicJsonType, typename T, std::size_t N, - enable_if_t < !std::is_default_constructible>::value, int > = 0 > -auto from_json(BasicJsonType && j, identity_tag> tag) --> decltype(j.template get(), -from_json_array_impl(std::forward(j), tag, make_index_sequence {})) +template < typename BasicJsonType, typename T, std::size_t N > +auto from_json_inplace_array_impl(BasicJsonType&& j, identity_tag> tag, priority_tag<0> /*unused*/) +-> decltype(from_json_inplace_array_impl_base(std::forward(j), tag, make_index_sequence {})) +{ + return from_json_inplace_array_impl_base(std::forward(j), tag, make_index_sequence {}); +} + +template < typename BasicJsonType, typename ArrayType > +auto from_json(BasicJsonType&& j, identity_tag tag) +-> decltype(from_json_inplace_array_impl(std::forward(j), tag, priority_tag<0> {})) { if (JSON_HEDLEY_UNLIKELY(!j.is_array())) { @@ -267,7 +273,7 @@ from_json_array_impl(std::forward(j), tag, make_index_sequence std::string(j.type_name()))); } - return from_json_array_impl(std::forward(j), tag, make_index_sequence {}); + return from_json_inplace_array_impl(std::forward(j), tag, priority_tag<0> {}); } template @@ -352,7 +358,6 @@ std::pair from_json_pair_impl(BasicJsonType&& j, identity_tag(j).at(1).template get()}; } - template>::value, int> = 0> void from_json_pair_impl(BasicJsonType && j, std::pair& p, priority_tag<1> /*unused*/) diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index f3c1b987c..b452ca713 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -3746,17 +3746,23 @@ void()) from_json_array_impl(j, arr, priority_tag<3> {}); } -template < typename BasicJsonType, typename Array, std::size_t... Is > -Array from_json_array_impl(BasicJsonType&& j, identity_tag /*unused*/, index_sequence /*unused*/) +template < typename T, typename BasicJsonType, typename ArrayType, std::size_t... Idx> +ArrayType from_json_inplace_array_impl_base(BasicJsonType&& j, identity_tag /*unused*/, + index_sequence /*unused*/) { - return { std::forward(j).at(Is).template get()... }; + return { std::forward(j).at(Idx).template get()... }; } -template < typename BasicJsonType, typename T, std::size_t N, - enable_if_t < !std::is_default_constructible>::value, int > = 0 > -auto from_json(BasicJsonType && j, identity_tag> tag) --> decltype(j.template get(), -from_json_array_impl(std::forward(j), tag, make_index_sequence {})) +template < typename BasicJsonType, typename T, std::size_t N > +auto from_json_inplace_array_impl(BasicJsonType&& j, identity_tag> tag, priority_tag<0> /*unused*/) +-> decltype(from_json_inplace_array_impl_base(std::forward(j), tag, make_index_sequence {})) +{ + return from_json_inplace_array_impl_base(std::forward(j), tag, make_index_sequence {}); +} + +template < typename BasicJsonType, typename ArrayType > +auto from_json(BasicJsonType&& j, identity_tag tag) +-> decltype(from_json_inplace_array_impl(std::forward(j), tag, priority_tag<0> {})) { if (JSON_HEDLEY_UNLIKELY(!j.is_array())) { @@ -3764,7 +3770,7 @@ from_json_array_impl(std::forward(j), tag, make_index_sequence std::string(j.type_name()))); } - return from_json_array_impl(std::forward(j), tag, make_index_sequence {}); + return from_json_inplace_array_impl(std::forward(j), tag, priority_tag<0> {}); } template @@ -3849,7 +3855,6 @@ std::pair from_json_pair_impl(BasicJsonType&& j, identity_tag(j).at(1).template get()}; } - template>::value, int> = 0> void from_json_pair_impl(BasicJsonType && j, std::pair& p, priority_tag<1> /*unused*/) diff --git a/test/src/unit-regression2.cpp b/test/src/unit-regression2.cpp index 1046bb2e5..e31296fac 100644 --- a/test/src/unit-regression2.cpp +++ b/test/src/unit-regression2.cpp @@ -543,9 +543,6 @@ TEST_CASE("regression tests 2") { { json j = { 3, 8 }; - auto x = j.at(0).get(); - CHECK(x.x == 3); - auto p = j.get>(); CHECK(p.first.x == 3); CHECK(p.second.x == 8);