From 467f622c65daaabc71ca0572c2a9764d0a187197 Mon Sep 17 00:00:00 2001 From: Jaakko Moisio Date: Mon, 28 Dec 2020 22:20:37 +0100 Subject: [PATCH] Fix compilation of input_adapter(container) in edge cases This fixes a compilation issue with the library if trying to use containers that don't have non-member `begin()` and `end()` functions via ADL. This patch extends the `using std::begin` and `using std::end` declarations to also cover the return type deduction of the input_adapter() template specialization for containers. The previous implementation only enabled the detection of `std::begin()` and `std::end()` in the function body, making the specialization unusable for container types that only have member `begin()` and `end()` functions. It is not typical to have `using` declarations in the namespace scope in a header file. But a C++11 implementation can't rely on fully automatic return type deduction, and needs to rely on ADL enabled helper templates. To prevent the using declarations leaking, they are enclosed in another nested namespace. --- .../nlohmann/detail/input/input_adapters.hpp | 35 +++++++++++++---- single_include/nlohmann/json.hpp | 39 ++++++++++++++----- test/src/unit-user_defined_input.cpp | 27 ++++++++++++- 3 files changed, 84 insertions(+), 17 deletions(-) diff --git a/include/nlohmann/detail/input/input_adapters.hpp b/include/nlohmann/detail/input/input_adapters.hpp index 63921ca55..eed370fd2 100644 --- a/include/nlohmann/detail/input/input_adapters.hpp +++ b/include/nlohmann/detail/input/input_adapters.hpp @@ -371,14 +371,35 @@ typename iterator_input_adapter_factory::adapter_type input_adapte } // Convenience shorthand from container to iterator -template -auto input_adapter(const ContainerType& container) -> decltype(input_adapter(begin(container), end(container))) -{ - // Enable ADL - using std::begin; - using std::end; +// Enables ADL on begin(container) and end(container) +// Encloses the using declarations in namespace for not to leak them to outside scope - return input_adapter(begin(container), end(container)); +namespace container_input_adapter_factory_impl { + +using std::begin; +using std::end; + +template +struct container_input_adapter_factory {}; + +template +struct container_input_adapter_factory< ContainerType, + void_t()), end(std::declval()))> > +{ + using adapter_type = decltype(input_adapter(begin(std::declval()), end(std::declval()))); + + static adapter_type create(const ContainerType& container) + { + return input_adapter(begin(container), end(container)); + } +}; + +} + +template +typename container_input_adapter_factory_impl::container_input_adapter_factory::adapter_type input_adapter(const ContainerType& container) +{ + return container_input_adapter_factory_impl::container_input_adapter_factory::create(container); } // Special cases with fast paths diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index 51fe8382b..a919b8d6c 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -5183,14 +5183,35 @@ typename iterator_input_adapter_factory::adapter_type input_adapte } // Convenience shorthand from container to iterator -template -auto input_adapter(const ContainerType& container) -> decltype(input_adapter(begin(container), end(container))) -{ - // Enable ADL - using std::begin; - using std::end; +// Enables ADL on begin(container) and end(container) +// Encloses the using declarations in namespace for not to leak them to outside scope - return input_adapter(begin(container), end(container)); +namespace container_input_adapter_factory_impl { + +using std::begin; +using std::end; + +template +struct container_input_adapter_factory {}; + +template +struct container_input_adapter_factory< ContainerType, + void_t()), end(std::declval()))> > +{ + using adapter_type = decltype(input_adapter(begin(std::declval()), end(std::declval()))); + + static adapter_type create(const ContainerType& container) + { + return input_adapter(begin(container), end(container)); + } +}; + +} + +template +typename container_input_adapter_factory_impl::container_input_adapter_factory::adapter_type input_adapter(const ContainerType& container) +{ + return container_input_adapter_factory_impl::container_input_adapter_factory::create(container); } // Special cases with fast paths @@ -16801,7 +16822,7 @@ class basic_json detail::parser_callback_tcb = nullptr, const bool allow_exceptions = true, const bool ignore_comments = false - ) + ) { return ::nlohmann::detail::parser(std::move(adapter), std::move(cb), allow_exceptions, ignore_comments); @@ -25346,7 +25367,7 @@ template<> inline void swap(nlohmann::json& j1, nlohmann::json& j2) noexcept( is_nothrow_move_constructible::value&& is_nothrow_move_assignable::value - ) +) { j1.swap(j2); } diff --git a/test/src/unit-user_defined_input.cpp b/test/src/unit-user_defined_input.cpp index 5a1138b00..4b84e8e71 100644 --- a/test/src/unit-user_defined_input.cpp +++ b/test/src/unit-user_defined_input.cpp @@ -63,7 +63,7 @@ const char* end(const MyContainer& c) return c.data + strlen(c.data); } -TEST_CASE("Custom container") +TEST_CASE("Custom container non-member begin/end") { MyContainer data{"[1,2,3,4]"}; @@ -75,6 +75,31 @@ TEST_CASE("Custom container") } +TEST_CASE("Custom container member begin/end") +{ + struct MyContainer2 + { + const char* data; + + const char* begin() const + { + return data; + } + + const char* end() const + { + return data + strlen(data); + } + }; + + MyContainer2 data{"[1,2,3,4]"}; + json as_json = json::parse(data); + CHECK(as_json.at(0) == 1); + CHECK(as_json.at(1) == 2); + CHECK(as_json.at(2) == 3); + CHECK(as_json.at(3) == 4); +} + TEST_CASE("Custom iterator") { const char* raw_data = "[1,2,3,4]";