Skip to content

ADL issue in input_adapter #2248

@mika-fischer

Description

@mika-fischer

json::parse does not work with containers which only provide begin() and end() member functions. This worked in 3.7.0.

Example https://godbolt.org/z/NtXn4M:

#if 1
#include "https://raw.githubusercontent.com/nlohmann/json/v3.8.0/single_include/nlohmann/json.hpp"
#else
#include "https://raw.githubusercontent.com/nlohmann/json/v3.7.0/single_include/nlohmann/json.hpp"
#endif

struct container {
  std::string data;              
  auto begin() const { return data.begin(); }
  auto end()   const { return data.end();   }
};

auto foo(container& c) { return nlohmann::json::parse(c); }

gives a (long) compile error.

This is probably because of the unqualified begin and end in the trailing return type here (https://github.com/nlohmann/json/blob/v3.8.0/single_include/nlohmann/json.hpp#L4796-L4805) without using std::begin; using std::end;:

template<typename ContainerType>
 auto input_adapter(const ContainerType& container) -> decltype(input_adapter(begin(container), end(container)))
 {
     // Enable ADL
     using std::begin;
     using std::end;
  
     return input_adapter(begin(container), end(container));
 }

The old code had std::begin and std::end there, which is also wrong, for different reasons. Unfortunately, I don't know how to fix it...

Metadata

Metadata

Assignees

No one assigned

    Labels

    kind: bugstate: stalethe issue has not been updated in a while and will be closed automatically soon unless it is updated

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions