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

std::unordered_map in struct, serialization #1133

Closed
014972304505347 opened this issue Jun 15, 2018 · 3 comments
Closed

std::unordered_map in struct, serialization #1133

014972304505347 opened this issue Jun 15, 2018 · 3 comments

Comments

@014972304505347
Copy link

014972304505347 commented Jun 15, 2018

  • What is the issue you have?

Cannot build deserialization code for a std::unordered_map. Following the example in the README at https://github.com/nlohmann/json#arbitrary-types-conversions

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

    #include <unordered_map>
    #include <vector>
    #include <string>
    #include <nlohmann/json.hpp>
    
    struct myStruct{
      std::string structName;
    }
      		
    struct myStructCollection {
      std::unordered_map<int, myStruct> myStructs;
      myStructCollection(std::unordered_map<int, myStruct> myStructs) : myStructs (myStructs){}
    };
    
    void from_json(const json& j, myStructCollection& p) {
      p.myStructs = j.at("myStructs").get<std::unordered_map<int, myStruct>>();
    }
    
  • What is the expected behavior?

Builds. According to https://github.com/nlohmann/json#conversion-from-stl-containers, std::unordered_map is supported.

  • And what is the actual behavior instead?

Error at line 269 of "utility":

  pair& operator=(pair&& _Right)
    _NOEXCEPT_COND(is_nothrow_move_assignable_v<_Ty1>
    && is_nothrow_move_assignable_v<_Ty2>)
  {	// assign from moved pair
    first = _STD forward<_Ty1>(_Right.first); // ERROR ON THIS LINE!!!!
    second = _STD forward<_Ty2>(_Right.second);
    return (*this);
  }

Error C2166 "l-value specifies const object "

MSVC compiler 19.14.26430, Windows 10, Visual Studio 2017 (version 15.7.2), v141 toolset.

  • Did you use a released version of the library or the version from the develop branch?

Used vcpkg to install.

@theodelrieu
Copy link
Contributor

It should be fixed on the develop branch, could you try it out?

@014972304505347
Copy link
Author

I just got the latest on the develop branch, and am still having the same issue.
Just to confirm - it is headers only?

@theodelrieu
Copy link
Contributor

Yes the library is header only. I did fix the issue for std::map, but not for std::unordered_map. Will open a PR next week.

theodelrieu added a commit to theodelrieu/json that referenced this issue Jun 18, 2018
theodelrieu added a commit to theodelrieu/json that referenced this issue Jun 18, 2018
theodelrieu added a commit to theodelrieu/json that referenced this issue Jun 18, 2018
theodelrieu added a commit to theodelrieu/json that referenced this issue Jun 18, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants