Skip to content

Implicit assignment to std::string fails #144

Closed

Description

@Cleroth

Put simply, when constructing a string, you can pass a basic_json:

std::string s = o["name"];

However, this does not work:

std::string s;
s = o["name"];
s = o["name"].get<std::string>(); // this works

The following compiler errors pop up:

1>g:\files\smoldy_adventure\src\client\units\unitmgr.cpp(53): error C2593: 'operator =' is ambiguous
1> g:\program files (x86)\microsoft visual studio 14.0\vc\include\xstring(1032): note: could be 'std::basic_string<char,std::char_traits,std::allocator> &std::basic_string<char,std::char_traits,std::allocator>::operator =(_Elem)'
1> with
1> [
1> _Elem=char
1> ] (compiling source file units\unitmgr.cpp)
1> g:\program files (x86)\microsoft visual studio 14.0\vc\include\xstring(1027): note: or 'std::basic_string<char,std::char_traits,std::allocator> &std::basic_string<char,std::char_traits,std::allocator>::operator =(const _Elem *)'
1> with
1> [
1> _Elem=char
1> ] (compiling source file units\unitmgr.cpp)
1> g:\program files (x86)\microsoft visual studio 14.0\vc\include\xstring(1011): note: or 'std::basic_string<char,std::char_traits,std::allocator> &std::basic_string<char,std::char_traits,std::allocator>::operator =(const std::basic_string<char,std::char_traits,std::allocator> &)' (compiling source file units\unitmgr.cpp)
1> g:\program files (x86)\microsoft visual studio 14.0\vc\include\xstring(972): note: or 'std::basic_string<char,std::char_traits,std::allocator> &std::basic_string<char,std::char_traits,std::allocator>::operator =(std::initializer_list<_Elem>)'
1> with
1> [
1> _Elem=char
1> ] (compiling source file units\unitmgr.cpp)
1> g:\program files (x86)\microsoft visual studio 14.0\vc\include\xstring(914): note: or 'std::basic_string<char,std::char_traits,std::allocator> &std::basic_string<char,std::char_traits,std::allocator>::operator =(std::basic_string<char,std::char_traits,std::allocator> &&) noexcept' (compiling source file units\unitmgr.cpp)
1> g:\files\smoldy_adventure\src\client\units\unitmgr.cpp(53): note: while trying to match the argument list '(std::string, stlx::basic_jsonstd::map,std::vector,std::string,bool,int64_t,double,std::allocator)'

Compiler: Visual Studio 2015

Activity

  1. nlohmann commented on Nov 2, 2015

    @nlohmann
    Owner

    Thanks for reporting! I'll check the issue soon.

  2. self-assigned this
    on Dec 6, 2015
  3. nlohmann commented on Dec 6, 2015

    @nlohmann
    Owner

    Unfortunately, I cannot reproduce the error with GCC or Clang. As I have no access to MSVC, I cannot dig deeper into the error. Do you have any idea what the reason could be that MSVC rejects the code?

  4. removed their assignment
    on Dec 6, 2015
  5. Cleroth commented on Dec 6, 2015

    @Cleroth
    Author

    I couldn't really find the reason. I'll check again in a bit.
    Meanwhile, VS 2015 is free, and if you don't want the IDE you can get just the compiler: http://blogs.msdn.com/b/vcblog/archive/2015/11/02/announcing-visual-c-build-tools-2015-standalone-c-tools-for-build-environments.aspx

  6. nlohmann commented on Dec 6, 2015

    @nlohmann
    Owner

    I am not using a Windows system, so trying MSVC is still difficult :-)

  7. ZahlGraf commented on Dec 6, 2015

    @ZahlGraf

    I can confirm this issue with MSVC 2015. The workaround with get<std::string>() works well for me.

  8. Cleroth commented on Dec 16, 2015

    @Cleroth
    Author

    Why?

  9. nlohmann commented on Dec 16, 2015

    @nlohmann
    Owner

    I am unable to debug MSVC error messages. If you have any idea how to fix it, I would be happy.

  10. Cleroth commented on Dec 16, 2015

    @Cleroth
    Author

    The problem is that basic_json can convert to both a char and a std::string, so MSVC doesn't know which function to call. The only solution I've found is to prevent implicit conversion to char (ie. by adding typename = std::enable_if<!std::is_same<ValueType, char>::value>::type to operator ValueType()).
    You could probably easily replicate this by creating a dummy class with two operator= templates, one which takes a char, and the other a std::string.
    I'm curious as to how this works in GCC/Clang... How are they implementing the std::string::operator=?

  11. 9 remaining items

  12. erichkeane commented on Dec 30, 2015

    @erichkeane

    @nlohmann : I checked out master with a VM, and get the same issue on Master, so I would say that the issue is NOT with the proposed patch, but with master itself.

  13. Cleroth commented on Dec 30, 2015

    @Cleroth
    Author

    The patch is on master, I believe.

  14. erichkeane commented on Dec 30, 2015

    @erichkeane

    @Cleroth: I mean the latest issue that he's having in AppVeyor. Even without the std::string::operator= stuff, I get the issue of an ambigious operator <<.

    I filed bug #167 to comprehend the issue that is blocking this currently.

  15. twelsby commented on Jan 22, 2016

    @twelsby
    Contributor

    This is fixed in pull #183 - see issue #167.

  16. nlohmann commented on Jan 24, 2016

    @nlohmann
    Owner

    @twelsby, do I understand correctly that this issue was solved by PR #188?

  17. added
    state: please discuss
    on Jan 24, 2016
  18. twelsby commented on Jan 25, 2016

    @twelsby
    Contributor

    @nlohmann. yes. Actually it was fixed in 457bfc2 but that created side effects that were fixed in PR #188. As that pull has now been merged, the current situation on master is that this is fixed. You should be able to close this issue.

  19. removed
    state: please discuss
    on Jan 26, 2016
  20. added this to the Release 1.1.0 milestone on Jan 26, 2016
  21. nlohmann commented on Jan 26, 2016

    @nlohmann
    Owner

    Thanks!

  22. thomasdullien commented on Dec 16, 2016

    @thomasdullien

    Hey there,

    it seems the issue is cropping up for me again. I am trying to compile the same project on GCC and on Clang (built from current trunk), and the unit test compilation is failing for issue 144:

    /XXX/project-zero/thomasdullien/XXX/json-src/test/src/unit-regression.cpp:250:12: error:
    use of overloaded operator '=' is ambiguous (with operand types 'std::string' (aka 'basic_string<char,
    char_traits, allocator >') and 'value_type' (aka 'nlohmann::basic_json<std::map, std::vector,
    std::__1::basic_string, bool, long, unsigned long, double, std::allocator>'))
    s2 = o["name"];
    ~~ ^ ~~~~~~~~~
    /usr/local/bin/../include/c++/v1/string:825:19: note: candidate function
    basic_string& operator=(basic_string&& __str)
    ^
    /usr/local/bin/../include/c++/v1/string:820:19: note: candidate function
    basic_string& operator=(const basic_string& __str);
    ^
    /usr/local/bin/../include/c++/v1/string:822:19: note: candidate function
    basic_string& operator=(__self_view __sv) {return assign(__sv);}
    ^
    1 error generated.
    make[2]: *** [json-build/test/CMakeFiles/json_unit.dir/src/unit-regression.cpp.o] Error 1
    make[1]: *** [json-build/test/CMakeFiles/json_unit.dir/all] Error 2
    make: *** [all] Error 2

    What's the best way to help fixing this? :) (btw, big fan of your library)

  23. nlohmann commented on Dec 16, 2016

    @nlohmann
    Owner

    That is strange, because this regression test is run with every commit on AppVeyor, see https://ci.appveyor.com/project/nlohmann/json/history.

    Edit: Of course, I meant Travis, see https://travis-ci.org/nlohmann/json/branches

  24. jaredgrubb commented on Dec 16, 2016

    @jaredgrubb
    Contributor

    Maybe it's a C++17 thing? The "self view" sounds a bit like the new string view.

  25. thomasdullien commented on Dec 16, 2016

    @thomasdullien
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions