-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Allow Json::Value to be used in a boolean context #695
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
Allow Json::Value to be used in a boolean context #695
Conversation
In a boolean context, a Value is "false" if it is null, and "true" if it is not null. This was already implemented partially by operator! and is now supported fully by adding implicit conversion of Value to bool (through "operator bool"). For example: Json::Value v; ... // We already had this before (through operator!): if (!v) { // Executed if v is null } // This commit adds the following (through the new operator bool): if (v) { // Executed if v is not null } Includes test coverage.
Hi, any comments from the maintainers about this change? Did you decide whether or not to merge it yet? |
Makes sense to me.
Also, if you have an operator bool you get operator! for free. So we don’t
need it anymore except for ABI compatibility I guess.
On Mon, Nov 13, 2017 at 8:11 AM Wolfram Rösler ***@***.***> wrote:
Hi, any comments from the maintainers about this change? Did you decide
whether or not to merge it yet?
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
<#695 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AHFzPEf-q0dNEQ4yjVlDlG8TUfMJDPlYks5s2D_jgaJpZM4QHQCv>
.
--
ǝnɥɐuop ʎllıq
|
@BillyGoto, my instinct is to require a bool conversion to be explicit, but I will defer to your judgement. @wolframroesler, please also remove |
On Thu, Nov 16, 2017 at 1:59 PM Christopher Dunn ***@***.***> wrote:
@BillyGoto <https://github.com/billygoto>, my instinct is to require a
bool conversion to be explicit, but I will defer to your judgement.
Definitely explicit
@wolframroesler <https://github.com/wolframroesler>, please also remove
operator!() per Billy's suggestion. Sorry for delay in response.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#695 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AHFzPGmqFr_w2AAPT5aNYdmBED5xYy0xks5s3IXogaJpZM4QHQCv>
.
--
ǝnɥɐuop ʎllıq
|
It's no longer needed since we now have operator bool which gives us operator! for free.
Thanks for your input. Removed operator! (the PR now essentially replaces operator! with operator bool). |
Hi maintainers, how about merging it? I dearly need it :) but I don't want to change it in my copy of the repository if it's not going into the official distribution. Thanks! |
@wolframroesler, two of us want |
Sorry, completely missed those messages. Making the operator |
We want to do things like: Json::Value v; ... if (v) ...; if (!v) ...; but not bool b = v; so the Value-to-bool conversion operator has to be marked `explicit`.
We'll have to bump the soversion before we tag a new release. Thanks for the changes. |
I just noticed this in a TravisCI build: ../include/json/value.h:404:28: warning: explicit conversion operators only available with -std=c++11 or -std=gnu++11
explicit operator bool() const;
^ |
jsoncpp 1.* is for C++ 11, isn't it? Is the C++ 11 compiler option set in the Travis build? If it is, is the warning of any significance? If it isn't, can it be disabled via a compiler option? |
We set this is the Meson build to eliminate warnings, but c++0x should still work, at least for now. See open-source-parsers#695 for discussion.
We set this is the Meson build to eliminate warnings, but c++0x should still work, at least for now. See #695 for discussion.
This commit has broken the support for Visual Studio 2012, which doesn't support |
Very sorry to hear that. Since I don't have Visual Studio, would you mind submitting a pull request with the code modified as you suggest? |
OK, will do so. |
Issue #731 has been created to cope with that problem. |
In a boolean context, a Value is
false
if it is null, andtrue
if it is not null. This was already implemented partially byoperator!
and is now supported fully by adding implicit conversion of Value to bool (throughoperator bool
).For example:
Includes test coverage.