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

Overload parse() to accept an rvalue reference #120

Merged
merged 1 commit into from
Oct 3, 2015

Conversation

silverweed
Copy link
Contributor

Currently, one cannot directly pass a std::ifstream to the parse function:

const char *filename = "my_file.json";
json my_json = json::parse(std::ifstream(filename));

This fails with the error:

error: invalid initialization of non-const reference of type ‘std::istream& {aka std::basic_istream<char>&}’ from an rvalue of type ‘std::basic_istream<char>’
 json my_json = json::parse(std::ifstream(filename));

If I'm not mistaken, this happens because a temporary ifstream is created which cannot be bound to a non-const reference, as it's required from the signature of parse(std::istream&, parser_callback_t).

Overloading parse to accept an rvalue reference (valid C++11) solves the problem and allows to move the ownership of the temporary stream to the function.

@nlohmann
Copy link
Owner

Hi @silverweed, thanks for the commit! Could you please make the change to json.hpp.re2c, because json.hpp is created from this file?

@silverweed
Copy link
Contributor Author

Ok, I updated the PR (this time I only modified json.hpp.re2c, I hope I didn't have to leave json.hpp modified too - if this is the case, tell me and I'll edit it back ;-)

nlohmann added a commit that referenced this pull request Oct 3, 2015
Overload parse() to accept an rvalue reference
@nlohmann nlohmann merged commit 589dc21 into nlohmann:master Oct 3, 2015
@nlohmann
Copy link
Owner

nlohmann commented Oct 3, 2015

Thanks a lot!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants