Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
1 of 5 tasks
HarryDC opened this issue Oct 8, 2020 · 2 comments
Closed
1 of 5 tasks

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

HarryDC opened this issue Oct 8, 2020 · 2 comments

Comments

@HarryDC
Copy link

HarryDC commented Oct 8, 2020

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
@gregmarr
Copy link
Contributor

This error is the reason that uniform initialization syntax was introduced in C++11. Change

    std::ifstream in(std::string(filename));

to

    std::ifstream in{std::string(filename)};

Here's why: https://en.wikipedia.org/wiki/Most_vexing_parse

@HarryDC
Copy link
Author

HarryDC commented Oct 12, 2020

Sigh, yes ... thanks

@HarryDC HarryDC closed this as completed Oct 12, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants