Repository navigation
Description
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
nlohmann commentedon Nov 2, 2015
Thanks for reporting! I'll check the issue soon.
nlohmann commentedon Dec 6, 2015
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?
Cleroth commentedon Dec 6, 2015
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
nlohmann commentedon Dec 6, 2015
I am not using a Windows system, so trying MSVC is still difficult :-)
ZahlGraf commentedon Dec 6, 2015
I can confirm this issue with MSVC 2015. The workaround with
get<std::string>()works well for me.Cleroth commentedon Dec 16, 2015
Why?
nlohmann commentedon Dec 16, 2015
I am unable to debug MSVC error messages. If you have any idea how to fix it, I would be happy.
Cleroth commentedon Dec 16, 2015
The problem is that
basic_jsoncan convert to both acharand astd::string, so MSVC doesn't know which function to call. The only solution I've found is to prevent implicit conversion tochar(ie. by addingtypename = std::enable_if<!std::is_same<ValueType, char>::value>::typetooperator ValueType()).You could probably easily replicate this by creating a dummy class with two
operator=templates, one which takes achar, and the other astd::string.I'm curious as to how this works in GCC/Clang... How are they implementing the
std::string::operator=?9 remaining items
erichkeane commentedon Dec 30, 2015
@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.
Cleroth commentedon Dec 30, 2015
The patch is on master, I believe.
erichkeane commentedon Dec 30, 2015
@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.
twelsby commentedon Jan 22, 2016
This is fixed in pull #183 - see issue #167.
nlohmann commentedon Jan 24, 2016
@twelsby, do I understand correctly that this issue was solved by PR #188?
twelsby commentedon Jan 25, 2016
@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.
nlohmann commentedon Jan 26, 2016
Thanks!
thomasdullien commentedon Dec 16, 2016
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)
nlohmann commentedon Dec 16, 2016
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
jaredgrubb commentedon Dec 16, 2016
Maybe it's a C++17 thing? The "self view" sounds a bit like the new string view.
thomasdullien commentedon Dec 16, 2016