Talk:cpp/string/basic string/operator=/Archive 1
The g++ 4.8.2 gets confused on the following code (case 4 of basic_string operator=)
char l_chChar = 'a'; string l_strString = l_chChar; assert (l_strString == "a");
and treats the case 4 (a single character as an rvalue to a std::string operator=) as a const char pointer and generates the following errors:
teststrings.cpp:95:51: error: invalid conversion from ‘char’ to ‘const char*’ [-fpermissive]
char l_chChar = 'a'; string l_strString = l_chChar; assert (l_strString == "a");
^
In file included from /opt/gcc-4.8.2/include/c++/4.8.2/string:52:0,
from /opt/gcc-4.8.2/include/c++/4.8.2/bits/locale_classes.h:40,
from /opt/gcc-4.8.2/include/c++/4.8.2/bits/ios_base.h:41,
from /opt/gcc-4.8.2/include/c++/4.8.2/ios:42,
from /opt/gcc-4.8.2/include/c++/4.8.2/ostream:38,
from /opt/gcc-4.8.2/include/c++/4.8.2/iostream:39,
from teststrings.cpp:11:
/opt/gcc-4.8.2/include/c++/4.8.2/bits/basic_string.h:490:7: error: initializing argument 1 of ‘std::basic_string <_CharT, _Traits, _Alloc>::basic_string(const _CharT*, const _Alloc&) [with _CharT = char; _Traits = std::char_traits <char>; _Alloc = std::allocator <char>]’ [-fpermissive]
basic_string(const _CharT* __s, const _Alloc& __a = _Alloc());
^
Tomstraub (talk) 05:47, 6 May 2014 (PDT)
- string l_strString = l_chChar; is not an assignment and does not involve
operator=
. It is an example of copy-initialization, and gcc is correct in rejecting it. --Cubbi (talk) 05:56, 6 May 2014 (PDT)
[edit] operator=(CharT)
As far as I can tell, none of the published standards define this overload. It should probably be marked as a non-standard extension or removed completely. 95.172.145.162 05:52, 10 June 2015 (PDT)