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

+= issue with multiple redirection. #2970

Closed
sreerajchundayil opened this issue Aug 24, 2021 · 1 comment
Closed

+= issue with multiple redirection. #2970

sreerajchundayil opened this issue Aug 24, 2021 · 1 comment
Labels
kind: question solution: proposed fix a fix for the issue has been proposed and waits for confirmation

Comments

@sreerajchundayil
Copy link

Doing += with js2["key"]["num"] is an error while runtime.

Error message is : what(): [json.exception.type_error.308] cannot use push_back() with number

But it works fine with js1["num"].
Why are they different, it's against the intuition I think?

And is the error message relevant with the operation we are doing?

    int main()
    {
      json js1,js2;
    
      js1["num"] = 0;
      js2["key"]["num"] = 0;
    
      js1["num"] += 10;//fine
      js2["key"]["num"] += 10;//error
    }
@nlohmann
Copy link
Owner

Already the first occurrence of += yields the same exception. The library uses operator+= as synonym of push_back, and it is not usable with numbers.

If you want to use += with the stored integer, you need to make it explicit:

int main()
{
    json js1,js2;
  
    js1["num"] = 0;
    js2["key"]["num"] = 0;
  
    js1["num"].get_ref<json::number_integer_t&>() += 10;
    js2["key"]["num"].get_ref<json::number_integer_t&>() += 10;
 
    std::cout << js1 << '\n' << js2 << std::endl;
}

Output:

{"num":10}
{"key":{"num":10}}

@nlohmann nlohmann added kind: question solution: proposed fix a fix for the issue has been proposed and waits for confirmation labels Aug 24, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind: question solution: proposed fix a fix for the issue has been proposed and waits for confirmation
Projects
None yet
Development

No branches or pull requests

2 participants