Skip to content

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

Closed
@ibkevg

Description

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)'

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions