json/include/nlohmann/detail
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
..
conversions add a switch to enable implicit conversions (defaults to true) 2020-07-22 10:49:01 +02:00
input Fix compilation of input_adapter(container) in edge cases 2020-12-25 15:08:55 +01:00
iterators ♻️ remove "#define private public" 2020-08-12 13:30:06 +02:00
meta Merge pull request #2533 from nlohmann/c++_future 2020-12-16 20:54:09 +01:00
output Apply suggestions from code review 2020-11-24 11:02:58 -08:00
exceptions.hpp 🚧 support for UBJSON high-precision numbers #2286 2020-07-20 13:57:19 +02:00
hash.hpp 🚨 remove -Wimplicit-fallthrough warning #2348 2020-08-10 20:48:39 +02:00
json_pointer.hpp ♻️ remove "#define private public" 2020-08-12 13:30:06 +02:00
json_ref.hpp 🎨 amalgamate code 2020-12-14 10:38:49 +01:00
macro_scope.hpp ♻️ remove "#define private public" 2020-08-12 13:30:06 +02:00
macro_unscope.hpp ♻️ remove "#define private public" 2020-08-12 13:30:06 +02:00
value_t.hpp 🔥 remove unused boolean_operators.hpp header 2020-07-11 19:20:44 +02:00