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

Implicit assignment to std::string fails #144

Closed
Cleroth opened this issue Nov 2, 2015 · 22 comments
Closed

Implicit assignment to std::string fails #144

Cleroth opened this issue Nov 2, 2015 · 22 comments

Comments

@Cleroth
Copy link

Cleroth commented Nov 2, 2015

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

@nlohmann
Copy link
Owner

nlohmann commented Nov 2, 2015

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

@nlohmann nlohmann self-assigned this Dec 6, 2015
@nlohmann nlohmann removed the confirmed label Dec 6, 2015
@nlohmann
Copy link
Owner

nlohmann commented 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?

@nlohmann nlohmann removed their assignment Dec 6, 2015
@Cleroth
Copy link
Author

Cleroth commented 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 nlohmann added the platform: visual studio related to MSVC label Dec 6, 2015
@nlohmann
Copy link
Owner

nlohmann commented Dec 6, 2015

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

@ZahlGraf
Copy link

ZahlGraf commented Dec 6, 2015

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

@Cleroth
Copy link
Author

Cleroth commented Dec 16, 2015

Why?

@nlohmann
Copy link
Owner

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

@Cleroth
Copy link
Author

Cleroth commented Dec 16, 2015

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=?

@Furcube
Copy link

Furcube commented Dec 19, 2015

GCC 5.3.0 on linux produces similar error. Need to use explicit conversion.

@Cleroth
Copy link
Author

Cleroth commented Dec 19, 2015

Clang 3.7 also reports the error.

@nlohmann nlohmann removed the platform: visual studio related to MSVC label Dec 19, 2015
@nlohmann nlohmann reopened this Dec 19, 2015
@nlohmann nlohmann self-assigned this Dec 20, 2015
nlohmann added a commit that referenced this issue Dec 20, 2015
@nlohmann
Copy link
Owner

I fixed the code (see 457bfc2) and added test cases. The code compiles for GCC and Clang, but AppVeyor (https://ci.appveyor.com/project/nlohmann/json) fails to compile the code with MSVC. Does anyone have an idea what could be the issue?

@erichkeane
Copy link

@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
Copy link
Author

Cleroth commented Dec 30, 2015

The patch is on master, I believe.

@erichkeane
Copy link

@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
Copy link
Contributor

twelsby commented Jan 22, 2016

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

@nlohmann
Copy link
Owner

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

@nlohmann nlohmann added the state: please discuss please discuss the issue or vote for your favorite option label Jan 24, 2016
@twelsby
Copy link
Contributor

twelsby commented 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 nlohmann removed the state: please discuss please discuss the issue or vote for your favorite option label Jan 26, 2016
@nlohmann nlohmann added this to the Release 1.1.0 milestone Jan 26, 2016
@nlohmann
Copy link
Owner

Thanks!

@thomasdullien
Copy link

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
Copy link
Owner

nlohmann commented 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
Copy link
Contributor

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

@thomasdullien
Copy link

thomasdullien commented Dec 16, 2016 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

8 participants