json/test/src
Jaakko Moisio 1c130315ac Fix compilation of input_adapter(container) in edge cases
This fixes a compilation issue with the library if trying to parse JSON from a
custom container type that doesn't support detecting `begin()` and `end()`
functions via ADL.

`include/nlohmann/detail/input/input_adapters.hpp` defines convenience function
for creating input adapter for STL-like containers. It should support iterators
both via `std::begin()` and `std::end()`, as well as `begin()` and `end()` found
via ADL. However, the former doesn't actually work for iterable types not
defined in the `std` namespace, due to the way the return type deduction is
implemented:

    $ cat test.cc
    #include <iostream>
    #include <nlohmann/json.hpp>

    const char DATA[] = R"("Hello, world!")";

    struct MyIterable {
        using iterator = const char*;
        using const_iterator = iterator;
        const_iterator begin() const { return DATA; }
        const_iterator end() const { return DATA + sizeof(DATA); }
    };

    int main()
    {
        const auto c = MyIterable {};
        std::cout << nlohmann::json::parse(c) << "\n";
        return 0;
    }
    $ g++ --version
    g++ (Ubuntu 10.2.0-13ubuntu1) 10.2.0
    Copyright (C) 2020 Free Software Foundation, Inc.
    This is free software; see the source for copying conditions.  There is NO
    warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

    $ g++ -std=c++11 test.cc
    ...
    /usr/include/nlohmann/detail/input/input_adapters.hpp:375:6: note: candidate: ‘template<class ContainerType> decltype (nlohmann::detail::input_adapter(begin(container), end(container))) nlohmann::detail::input_adapter(const ContainerType&)’
      375 | auto input_adapter(const ContainerType& container) -> decltype(input_adapter(begin(container), end(container)))
          |      ^~~~~~~~~~~~~
    /usr/include/nlohmann/detail/input/input_adapters.hpp:375:6: note:   template argument deduction/substitution failed:
    /usr/include/nlohmann/detail/input/input_adapters.hpp: In substitution of ‘template<class ContainerType> decltype (nlohmann::detail::input_adapter(begin(container), end(container))) nlohmann::detail::input_adapter(const ContainerType&) [with ContainerType = MyIterable]’:
    ...

In the above example the trailing type deduction won't look for the `begin()`
and `end()` functions in the `std` namespace, although the function body would.

I don't know about a more elegant fix in C++11 except to spell out the return
type verbatim.
2020-12-25 15:08:55 +01:00
..
fuzzer-driver_afl.cpp 🔖 set version to 3.9.1 2020-08-06 13:45:29 +02:00
fuzzer-parse_bson.cpp 🔖 set version to 3.9.1 2020-08-06 13:45:29 +02:00
fuzzer-parse_cbor.cpp 🔖 set version to 3.9.1 2020-08-06 13:45:29 +02:00
fuzzer-parse_json.cpp 🔖 set version to 3.9.1 2020-08-06 13:45:29 +02:00
fuzzer-parse_msgpack.cpp 🔖 set version to 3.9.1 2020-08-06 13:45:29 +02:00
fuzzer-parse_ubjson.cpp 🔖 set version to 3.9.1 2020-08-06 13:45:29 +02:00
test_utils.hpp 🚨 fix warning 2020-07-26 12:12:17 +02:00
UBSAN.supp .travis/cmake: Rework clang sanitizer invocation 2019-09-03 13:22:03 +02:00
unit-algorithms.cpp 🔖 set version to 3.9.1 2020-08-06 13:45:29 +02:00
unit-allocator.cpp ♻️ remove "#define private public" 2020-08-12 13:30:06 +02:00
unit-alt-string.cpp 🔖 set version to 3.9.1 2020-08-06 13:45:29 +02:00
unit-assert_macro.cpp 🔖 set version to 3.9.1 2020-08-06 13:45:29 +02:00
unit-bson.cpp 🚨 fix warnings 2020-12-16 21:44:35 +01:00
unit-capacity.cpp 🔖 set version to 3.9.1 2020-08-06 13:45:29 +02:00
unit-cbor.cpp 🚨 fix warnings 2020-12-16 21:44:35 +01:00
unit-class_const_iterator.cpp ♻️ remove "#define private public" 2020-08-12 13:30:06 +02:00
unit-class_iterator.cpp ♻️ remove "#define private public" 2020-08-12 13:30:06 +02:00
unit-class_lexer.cpp ♻️ remove "#define private public" 2020-08-12 13:30:06 +02:00
unit-class_parser.cpp 🚨 fix warnings 2020-12-16 21:44:35 +01:00
unit-comparison.cpp 🔖 set version to 3.9.1 2020-08-06 13:45:29 +02:00
unit-concepts.cpp 🔖 set version to 3.9.1 2020-08-06 13:45:29 +02:00
unit-constructor1.cpp ♻️ remove "#define private public" 2020-08-12 13:30:06 +02:00
unit-constructor2.cpp 🔖 set version to 3.9.1 2020-08-06 13:45:29 +02:00
unit-convenience.cpp ♻️ remove "#define private public" 2020-08-12 13:30:06 +02:00
unit-conversions.cpp Include <string_view> in "nlohmann/json.hpp" when C++17 is used 2020-12-20 19:40:36 +05:00
unit-deserialization.cpp 🚨 fix warnings 2020-12-16 21:44:35 +01:00
unit-element_access1.cpp 🔖 set version to 3.9.1 2020-08-06 13:45:29 +02:00
unit-element_access2.cpp 🔖 set version to 3.9.1 2020-08-06 13:45:29 +02:00
unit-hash.cpp 🔖 set version to 3.9.1 2020-08-06 13:45:29 +02:00
unit-inspection.cpp 🔖 set version to 3.9.1 2020-08-06 13:45:29 +02:00
unit-items.cpp Restore intentionally disrupted C++17 tests 2020-12-20 12:43:35 +05:00
unit-iterators1.cpp ♻️ remove "#define private public" 2020-08-12 13:30:06 +02:00
unit-iterators2.cpp 🔖 set version to 3.9.1 2020-08-06 13:45:29 +02:00
unit-json_patch.cpp 🔖 set version to 3.9.1 2020-08-06 13:45:29 +02:00
unit-json_pointer.cpp ♻️ remove "#define private public" 2020-08-12 13:30:06 +02:00
unit-large_json.cpp 🔖 set version to 3.9.1 2020-08-06 13:45:29 +02:00
unit-merge_patch.cpp 🔖 set version to 3.9.1 2020-08-06 13:45:29 +02:00
unit-meta.cpp 🔖 set version to 3.9.1 2020-08-06 13:45:29 +02:00
unit-modifiers.cpp 🔖 set version to 3.9.1 2020-08-06 13:45:29 +02:00
unit-msgpack.cpp 🔖 set version to 3.9.1 2020-08-06 13:45:29 +02:00
unit-noexcept.cpp 🔖 set version to 3.9.1 2020-08-06 13:45:29 +02:00
unit-ordered_json.cpp ordered_map::insert(InputIt first, InputIt last) is added 2020-12-07 20:15:41 +03:00
unit-ordered_map.cpp 🔖 set version to 3.9.1 2020-08-06 13:45:29 +02:00
unit-pointer_access.cpp 🔖 set version to 3.9.1 2020-08-06 13:45:29 +02:00
unit-readme.cpp 🔖 set version to 3.9.1 2020-08-06 13:45:29 +02:00
unit-reference_access.cpp 🔖 set version to 3.9.1 2020-08-06 13:45:29 +02:00
unit-regression1.cpp ♻️ remove "#define private public" 2020-08-12 13:30:06 +02:00
unit-regression2.cpp Restore intentionally disrupted C++17 tests 2020-12-20 12:43:35 +05:00
unit-serialization.cpp 🔖 set version to 3.9.1 2020-08-06 13:45:29 +02:00
unit-testsuites.cpp 🔖 set version to 3.9.1 2020-08-06 13:45:29 +02:00
unit-to_chars.cpp 🔖 set version to 3.9.1 2020-08-06 13:45:29 +02:00
unit-ubjson.cpp 🚨 fix warnings 2020-12-16 21:44:35 +01:00
unit-udt_macro.cpp 🔖 set version to 3.9.1 2020-08-06 13:45:29 +02:00
unit-udt.cpp 🔖 set version to 3.9.1 2020-08-06 13:45:29 +02:00
unit-unicode.cpp 🚨 fix warnings 2020-12-16 21:44:35 +01:00
unit-user_defined_input.cpp Fix compilation of input_adapter(container) in edge cases 2020-12-25 15:08:55 +01:00
unit-wstring.cpp 🔖 set version to 3.9.1 2020-08-06 13:45:29 +02:00
unit.cpp 🔖 set version to 3.9.1 2020-08-06 13:45:29 +02:00