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

initializer list constructor makes curly brace initialization fragile #1594

Closed
maddanio opened this issue May 2, 2019 · 5 comments
Closed

Comments

@maddanio
Copy link

maddanio commented May 2, 2019

Hey,

I just discovered when i convert something to json on the fly like this:
nlohmann::json{my_value}
it turns it into an array, which is highly unexpected. this is a bit like the operator T() problem: too muc magic. I would be less dangerous if array/object construction where explicit, i.e. json::array({....})/json::object({{key, value},...). one can even make json::array/object objects and lose the round braces.

@nlohmann
Copy link
Owner

nlohmann commented May 2, 2019

The problem is known, and changing it would be a breaking change...

@maddanio
Copy link
Author

maddanio commented May 2, 2019

As for operator T(), right? I would say it should be part of the same discussion. Or a version 2 started, which has less magic and better composbility?

@olavurmortensen
Copy link

Would probably be a good idea to mention this in the documentation. The Cpp Core Guidelines recommend always using value initialization, so I expect a lot of people are doing that. Had this problem today, and it took me a good while to locate it, and didn't fully understand it before finding this issue.

@falbrechtskirchinger
Copy link
Contributor

@olavurmortensen It is mentioned in the FAQ. Where did you look? In other words, where should it be mentioned to make it more discoverable? Probably at least the constructor, where I can't find it.

@olavurmortensen
Copy link

I've only read in the reference:
https://json.nlohmann.me/api/basic_json/parse/
and the README:
https://github.com/nlohmann/json#read-json-from-a-file
But I didn't see any mention of this there.

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

4 participants