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')
The key difference is that for the
operator +=
variant, the char type template argument for thestd::basic_string
, and thus the argument type for its RHS, is already fixed tochar
, while theoperator+
template has to deduce that from its arguments.Thus, for the
+=
case, the compiler knows you "want" theint
->char
conversion, there is nothing to deduce there.For the
operator+
case on the other hand, the compiler is looking at the templateand, when trying to determine what
CharT
is supposed to be, it getsCharT = char
from the first operand (asstd::string
isstd::basic_string<char>
) andCharT = int
from the second operand. Such a conflict is defined to be a compilation error by the standard.