-
-
Notifications
You must be signed in to change notification settings - Fork 6.8k
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
Arithmetic operators are not working as expected #3832
Comments
There are no operator overloads, so you're simply observing the interaction of implicit conversion with the default arithmetic operators. One more reason to disable implicit conversion. It's not immediately apparent to me where to best document this behavior. Another way to address this would be to add operator overloads and do the Right Thing:tm:. People will disagree on what that is when it comes to non-numeric types. (I can't find the issue that discussed adding |
Agree that no apparent place to mention this behavior. However, It would be great if the library provide the default arithmetic operators at least for json numeric types (e.g json::number_float_t). It is also natural since the operator |
I had some more time to think about this. Forwarding the operators to the underlying/stored type seems doable and sidesteps the question of what The only reason I still hesitate is the sheer number of operators: There are 13 arithmetic operators and 10 compound assignment operators. Each operator needs several implementations for the various combinations of types, but not all operators have to be implemented for every type (e.g., bitwise operators are only needed for integral types). These operator overloads should only be defined if implicit conversion is enabled ('JSON_USE_IMPLICIT_CONVERSIONS == 1`). |
Assuming from what you commented, it is required to implement the I am wondering if it is possible to overload operators that can be apply for any numeric types first. There are a lot of operators for the numeric type, but the only few the arithmetic / compound assignment operators exist to apply to any numeric types. Additionally, Implementing operators for objects needs many efforts to define what operators mean with everyone's agreement, and it needs some careful consideration. |
Currently, without any operator overloads defined, any arithmetic operation with a
It could, but I don't think that's a good idea for the reason stated above.
As long as all operators are implemented, I have no objections to supporting numeric types only. |
Since we've said that we want to eventually get rid of implicit conversions, I don't think adding operator overloads is a good thing. Even under |
@gregmarr I read the documentation regarding However, as long as For example, for the By the way, I am wondering the major reason to turn off the implicit conversions by default. This is was one of features I got impressed of, compared to other json parsing libraries. |
See #958. |
I have no problem with adding something to the docs. FYI, both Clang and GCC consider your sample to be ambiguous and fail to compile. |
@gregmarr Oh, I made a mistake when copying and pasting. I just edited it, but it is not related to ambiguity of operators. You mean the following example failed to compile in GCC? If the sample only works on the specific compilers, operator overloads for implicit conversion don't make sense.
|
Thanks, there were already careful, considerate discussion regarding this issue. There are caveats in the both options, but turning off the implicit conversion looks like a better option for stability and maintenance of the library. I believe adding operator overloads when implicit conversion is on only for numeric types can mitigate this issue.... @gregmarr a lot of issues in #958 were from implicit conversion from complex types (e.g std vectors, user defined classes) There is one issue related to boolean type, but I believe this was caused since implicit conversion is on by default, but implicit conversion itself. |
I don't think investing effort into all these conversions makes sense. |
Agree. I just realized that it is a compiler dependent issue. Sorry for the confusion. |
Description
The default arithmetic operators, +, - and / for
json
object consider the value of the json object asint
regardless of the actual value type.This behavior is not specified in the documentation. It should be mentioned (or warned) in the documentation. This unexpected behavior from the default operators can easily mislead people.
Note: It can be naively fixed with the casting operator (e.g.
double(json_object)
)Reproduction steps
Straight forward. run the minimum reproducible example.
Expected vs. actual results
Expected Result
Actual Result
Minimal code example
Error messages
No response
Compiler and operating system
MSVC C++ 20, Windows 10
Library version
3.11.2
Validation
develop
branch is used.The text was updated successfully, but these errors were encountered: