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

json to concrete type conversion through reference or pointer fails #1361

Closed
ibkevg opened this issue Nov 15, 2018 · 2 comments
Closed

json to concrete type conversion through reference or pointer fails #1361

ibkevg opened this issue Nov 15, 2018 · 2 comments

Comments

@ibkevg
Copy link

ibkevg commented Nov 15, 2018

If I try to convert a json object into my concrete type through a reference, the compile fails claiming that operator= is ambiguous. Do you have any thoughts on why that might be the case and what I could do to work around it? I'd like to avoid having to do a 2 step, convert to local value then copy to where it needs to be.

edit: I've just discovered that the problem seems to be because only the copy constructor works, not the assignment operator. See "edit" in the code below. Is this by design?

    typedef std::map<std::string, std::string> MyMap;
    MyMap m1{ {"a", "A"}, {"b", "B"} };

    json j = m1;

#if 1
    MyMap m2 = j; // only this works
edit: #elif 0
edit:     MyMap m2;
edit:     m2 = j; // fails claiming = operator is ambiguous
#elif 0
    MyMap* m2 = new MyMap();
    *m2 = j; // fails claiming = operator is ambiguous
#elif 0
    MyMap m;
    MyMap& m2 = m;
    m2 = j; // fails claiming = operator is ambiguous
#elif 0
    auto m2 = std::make_shared<MyMap>();
    *m2 = j; // fails claiming = operator is ambiguous
#endif

    EXPECT_EQ(m1, m2);

The specific error is as follows (for the above case when using the pointer):

1>test.cpp
1>test.cpp(154): error C2593: 'operator =' is ambiguous
1>c:\program files (x86)\microsoft visual studio\2017\professional\vc\tools\msvc\14.16.27023\include\map(354): note: could be 'std::map<std::string,std::string,std::less<_Kty>,std::allocator<std::pair<const _Kty,_Ty>>> &std::map<_Kty,_Ty,std::less<_Ty>,std::allocator<std::pair<const _Kty,_Ty>>>::operator =(std::initializer_list<std::pair<const _Kty,_Ty>>)'
1>        with
1>        [
1>            _Kty=std::string,
1>            _Ty=std::string
1>        ]
1>c:\program files (x86)\microsoft visual studio\2017\professional\vc\tools\msvc\14.16.27023\include\map(189): note: or       'std::map<std::string,std::string,std::less<_Kty>,std::allocator<std::pair<const _Kty,_Ty>>> &std::map<_Kty,_Ty,std::less<_Ty>,std::allocator<std::pair<const _Kty,_Ty>>>::operator =(std::map<_Kty,_Ty,std::less<_Ty>,std::allocator<std::pair<const _Kty,_Ty>>> &&) noexcept(<expr>)'
1>        with
1>        [
1>            _Kty=std::string,
1>            _Ty=std::string
1>        ]
1>c:\program files (x86)\microsoft visual studio\2017\professional\vc\tools\msvc\14.16.27023\include\map(173): note: or       'std::map<std::string,std::string,std::less<_Kty>,std::allocator<std::pair<const _Kty,_Ty>>> &std::map<_Kty,_Ty,std::less<_Ty>,std::allocator<std::pair<const _Kty,_Ty>>>::operator =(const std::map<_Kty,_Ty,std::less<_Ty>,std::allocator<std::pair<const _Kty,_Ty>>> &)'
1>        with
1>        [
1>            _Kty=std::string,
1>            _Ty=std::string
1>        ]
1>test.cpp(154): note: while trying to match the argument list '(MyMap, json)'
@theodelrieu
Copy link
Contributor

You can find the details in #958. I advise to never use implicit conversion, and rather use j.get<T> or j.get_to.

@ibkevg
Copy link
Author

ibkevg commented Nov 16, 2018

Thank you I will add further comments in #958

@ibkevg ibkevg closed this as completed Nov 16, 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