-
-
Notifications
You must be signed in to change notification settings - Fork 6.9k
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
Feature request: Implicit conversion to bool #951
Comments
But implementing json j = false;
if (j) {
std::cout << "This output is surprising!\n";
} |
You're right, bool is a special case, and your example shouldn't produce any output. How about something like this (to be understood as pseudo-code rather than something that could be dropped into json.hpp unchanged): json::operator bool() const {
return is_boolean() ? get<bool>() : ! is_null();
} so bools work as expected, and for everything else, null is false and the rest is true. They may be situations where you really don't know what type a json object is storing, in which case you'd probably like to use Sounds intuitive, however that may also be because it's what I'm familiar with from another JSON library. Opinions? |
This is related to an issue I need to open about the implicit conversion operator While this operator exists, I think it is harmful to adopt such a change. I will go in more details as soon as I find some time to open an issue. |
I agree that implicit conversion is dangerous, that's why the C++ language avoids it whenever possible (think Of course, json j = /* object */;
bool b = j; won't work. We should ask ourselves which solution suits best into the general C++ design philosophy (into which the JSON library integrates seamlessly, which is a major reason why I like it). |
I agree, I personally would like to completely remove But while there's this dreadful operator, I believe it would be too dangerous to add the behavior you're proposing |
@theodelrieu do you have an example for unexpected things that would happen with the suggested bool conversion, provided that the existing |
Well first of all having You cannot know if what was returned was a correct boolean value, you have to call Even without the That's just my opinion right now, it might change in the future :) |
I am currently not really convinced that this operator would be helpful. However, it can be simulated with a function like bool to_bool(const json& j) {
return is_boolean() ? get<bool>() : ! is_null();
} in user code. It's not an operator, but it could still simplify your code. Any other ideas on this? |
I agree with @theodelrieu that the suggested Regarding the comparison with smart pointers made by @wolframroesler, I'm not sure that the situation is similar enough. With smart pointers it is quite clear what it means to be " The next person might want an empty array, and an empty object, perhaps even an empty string, to return When there is such ambiguity due to multiple valid choices I would argue that it is often better to not implement any one of them. This minimises the number of people being surprised by behaviour that doesn't match their expectation which, after all, would still be "a" valid one, even when not "the chosen one". |
Point taken. Issue revoked. Thanks for your explanations. |
What's the general opinion about allowing
json
objects in a boolean context? For example:if (j)
should be the same asif (!j.is_null())
, andif (!j)
should be the same asif (j.is_null())
.In the present version, the code above throws an exception ("[json.exception.type_error.302] type must be boolean, but is object").
The text was updated successfully, but these errors were encountered: