Skip to content

json::parse() can't be resolved under specific circumstances #2427

@HarryDC

Description

@HarryDC

What is the issue you have?

The following fails to compile

void doesnt_compile(std::string_view filename)
{
    std::ifstream in(std::string(filename));
    auto j  = nlohmann::json::parse(in);
}

even though this does compile

void  this_compiles(std::string filename)
{
    std::ifstream in(filename);
    auto j  = nlohmann::json::parse(in);
}

Please describe the steps to reproduce the issue.

See https://www.godbolt.org/z/GKr9hE

Can you provide a small but working code example?

void doesnt_compile(std::string_view filename)
{
    std::ifstream in(std::string(filename));
    auto j  = nlohmann::json::parse(in);
}

What is the expected behavior?

Should compile

And what is the actual behavior instead?

Under msvc the following error messages( excerpted)

Translator.cpp(19): note: Reason: cannot convert from 'overloaded-function' to 'nlohmann::detail::input_adapter'
Translator.cpp(19): note: No constructor could take the source type, or constructor overload resolution was ambiguous
Translator.cpp(23): error C2664: 'nlohmann::basic_json<std::map,std::vector,std::string,bool,int64_t,uint64_t,double,std::allocator,nlohmann::adl_serializer> nlohmann::basic_json<std::map,std::vector,std::string,bool,int64_t,uint64_t,double,std::allocator,nlohmann::adl_serializer>::parse(nlohmann::detail::input_adapter &&,const std::function<bool (int,nlohmann::detail::parser<nlohmann::basic_json<std::map,std::vector,std::string,bool,int64_t,uint64_t,double,std::allocator,nlohmann::adl_serializer>>::parse_event_t,BasicJsonType &)>,const bool)': cannot convert argument 1 from 'std::ifstream (__cdecl *)(std::string)' to 'nlohmann::detail::input_adapter &&'
1

gcc under compiler explorer has a similar error message

<source>: In function 'void doesnt_compile(std::string_view)':

<source>:17:39: error: no matching function for call to 'nlohmann::basic_json<>::parse(std::ifstream (&)(std::string))'

   17 |     auto j  = nlohmann::json::parse(in);

      |    

Which compiler and operating system are you using?

  • Compiler: MSVC 2017
  • Operating system: Windows

Which version of the library did you use?

  • latest release version 3.9.1
  • other release - please state the version: 3.6.0
  • the develop branch

If you experience a compilation error: can you compile and run the unit tests?

  • yes
  • no - please copy/paste the error message below

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions