考虑下面的代码。我不明白为什么我的 GCC 编译器不尝试隐式使用 Myclass::operator string(),尽管 Myclass::operator string() 已定义:
#include <string>
using namespace std;
struct T {
};
T operator+(const T& a, const T&b) { }
struct Myclass {
operator string() const { }
operator T() const { }
};
int main() {
T a;
string b;
Myclass c;
c + a; // OK
c.operator string() + b; // OK
c + b; // Not OK
/* The above line does not compile, although in <string> I see:
basic_string<_CharT, _Traits, _Alloc>
operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
const basic_string<_CharT, _Traits, _Alloc>& __rhs)
*/
}