-
Notifications
You must be signed in to change notification settings - Fork 2.7k
explicit operator bool() for Json::Value #228
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
Conversation
More convenient c++11 way to work with null Json::Value objects then isNull() or operator! http://en.wikibooks.org/wiki/More_C++_Idioms/Safe_bool#C.2B.2B11
include/json/value.h
Outdated
@@ -347,6 +347,23 @@ Json::Value obj_value(Json::objectValue); // {} | |||
/// Return isNull() | |||
bool operator!() const; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you provide explicit operator bool, then you get operator! for free.
The operand of operator! is contextually converted to bool. I wonder if this operator should be removed.
operator! is no longer necessary because of explicit operator bool()
This article explains the problems with
Really, |
Chris, are you considering specifically the request for http://stackoverflow.com/questions/6242768/is-the-safe-bool-idiom-obsolete-in-c11 |
I think we could define the There are no implicit conversions defined for
I think it's reasonable to follow the model of
|
Yes,
And really, I wish that @BillyDonahue, is this a big deal to you? If so, just push it. I'll add a revert for the |
I really want to be very clear that I don't think Json should bother with safebool hacks, and that
Here's an example of where you can't use
And another:
It's a bit of an impedance mismatch against modern C++, so I think we should just support the more modern style. Re "awful warts" I don't get it. I mean sure other C++ problems exist, but let's try to solve them one at a time. Sure there's a workaround, but really if something has a If we look at the landscape, we have a few type-erased objects in C++ these days as models. There's Thanks for being flexible, I do happen to think this is a step in the right direction. We can establish a notion that a |
Ok. Good examples and a convincing argument. Let's do it. But note that we will fully require C++11 for that branch. That's fine with me, and we can get serious about adding a move ctor, etc. |
* std::cin >> root; | ||
* | ||
* if (auto tag = root["tag"]) { | ||
* // Behavior if tag object with tag key exist |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This comment is probably overdoing it.
It's also got a grammatical problem and I don't like involving the behavior of operator[] into the explanation. Let's explain it without referring to other API points if we can.
Already merged as #695 |
More convenient c++11 way to work with null Json::Value objects then isNull() or operator!
http://en.wikibooks.org/wiki/More_C++_Idioms/Safe_bool#C.2B.2B11