有人问这个问题 https://stackoverflow.com/questions/45505477关于字符串附加。它是string s; s = s + 2;
不编译。人们给出的答案是operator+
被定义为模板函数,而operator+=
不是,所以自动向下转型(int(2)
to char(2)
)不适用。
原型是
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);
为什么编译器不能直接使用这个原型并将 int(2) 转换为 char(2) ?
basic_string<char, _T, _A> operator+(const basic_string<char, _T, _A>, char);
编译器(G++ 6.3.0)抱怨说
[Note] deduced conflicting types for parameter '_CharT' ('char' and 'int')