Someone asked this question about string appending. It's string s; s = s + 2;
not compiling. People gave answers stating that operator+
is defined as a template function while operator+=
is not, so auto downcasting (int(2)
to char(2)
) is not applied.
The prototypes are
template<typename _CharT, typename _Traits, typename _Alloc>
class basic_string{
basic_string&
operator+=(_CharT __c);
};
template<typename _CharT, typename _Traits, typename _Alloc>
inline basic_string<_CharT, _Traits, _Alloc>
operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs, _CharT __rhs);
Why can't the compiler just use this prototype and cast int(2) to char(2)?
basic_string<char, _T, _A> operator+(const basic_string<char, _T, _A>, char);
The compiler (G++ 6.3.0) complains that
[Note] deduced conflicting types for parameter '_CharT' ('char' and 'int')
basic_string
is supposed to support. Alsostring s; s = s + 2;
doesn't make much sense. Do you want the ascii character with the value of 2 or do you want'2'
? I can't see why your would need you version. – Jakie