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

Implicit type conversion error on MSVC #1333

Closed
kjpus opened this issue Nov 3, 2018 · 6 comments
Closed

Implicit type conversion error on MSVC #1333

kjpus opened this issue Nov 3, 2018 · 6 comments
Labels
platform: visual studio related to MSVC state: help needed the issue needs help to proceed state: stale the issue has not been updated in a while and will be closed automatically soon unless it is updated

Comments

@kjpus
Copy link
Contributor

kjpus commented Nov 3, 2018

  • What is the issue you have?
    Wrong type is deduced when json object is use in an expression with some other arithmetic types.

  • Please describe the steps to reproduce the issue. Can you provide a small but working code example?

// Compiled using MSVC 15.8.8
#include <iostream>
#include "nlohmann/json.hpp"

int main()
{
    nlohmann::json _j = R"({"Value":12.34})"_json;
    double x = _j["Value"]; // prints 12.34 here
    double y = _j["Value"] * 0.01; //prints 0.12 here
    std::cout << x << "  " << y << '\n';
    return 0;
}
  • What is the expected behavior?
    It should either fail to compile (as it does in gcc and clang), or deduce the correct type when mixed with arithmetic types in an expression and prints 12.34 and 0.1234.

  • And what is the actual behavior instead?
    MSVC compilation succeeds without error/warning (with /W4 and /permissive- flag). The second _j["Value"] calls the implicit conversion function with unsigned int type, converts it to double, then does the multiplication. It prints 12.34 and 0.12 instead.

  • Which compiler and operating system are you using? Is it a supported compiler?
    MSVC 15.8.8 on Windows 10, x64

  • Did you use a released version of the library or the version from the develop branch?
    3.3 (through vcpkg)

  • If you experience a compilation error: can you compile and run the unit tests?
    No compilation error.

@nlohmann nlohmann added the platform: visual studio related to MSVC label Nov 3, 2018
@nlohmann
Copy link
Owner

nlohmann commented Nov 3, 2018

I can confirm the code does not compile with Clang (tried Xcode 10). I also agree that the result right now is wrong, but I am not sure why MSVC behaves differently here.

@nlohmann nlohmann added the state: help needed the issue needs help to proceed label Nov 3, 2018
@kjpus
Copy link
Contributor Author

kjpus commented Nov 3, 2018

My current workaround is to provide operators for json type and an arithmetic type, e.g.

template <typename T, typename = std::enable_if_t<std::is_arithmetic_v<T>>> 
T operator*(const nlohmann::json& _j, T v)
{
    return _j.get<T>() * v;
}

It solves the problem but I'm wondering if there is a more elegant way.

Edit: The workaround only works for simple cases. json * json * double would fail.

Edit: Seems to be related to #958

@stale
Copy link

stale bot commented Dec 4, 2018

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the state: stale the issue has not been updated in a while and will be closed automatically soon unless it is updated label Dec 4, 2018
@theodelrieu
Copy link
Contributor

theodelrieu commented Dec 4, 2018

Please use auto v = _j["Value"].get<double>(); instead.

#1363 was merged recently to advise against using implicit conversions from a JSON value.

You can look at #958 for why that is recommended.

EDIT: Did not see you mentioned this issue in your edit already

@stale stale bot removed the state: stale the issue has not been updated in a while and will be closed automatically soon unless it is updated label Dec 4, 2018
@kjpus
Copy link
Contributor Author

kjpus commented Dec 4, 2018

Thanks for your response.

If implicit conversion is allowed but can potentially produce wrong result, it might be a good idea to disable it all together, at least for MSVC.

BTW, I created a PR as the link to #958 is broken in ReadMe.md.

@stale
Copy link

stale bot commented Jan 4, 2019

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the state: stale the issue has not been updated in a while and will be closed automatically soon unless it is updated label Jan 4, 2019
@stale stale bot closed this as completed Jan 15, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
platform: visual studio related to MSVC state: help needed the issue needs help to proceed state: stale the issue has not been updated in a while and will be closed automatically soon unless it is updated
Projects
None yet
Development

No branches or pull requests

3 participants