2012-04-07 16 views
5

Weź pod uwagę poniższy kod. Nie rozumiem, dlaczego mój kompilator GCC nie próbować niejawnie używać myClass :: string operator(), chociaż myClass :: string operator() jest zdefiniowana:Dlaczego konieczne jest jawne wywoływanie Myclass :: operator string() ze std :: string :: operator +()?

#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) 
    */ 
} 
+2

Wierzę, że powodem jest to, że 'std :: operator +' jest szablonem funkcji, a nie funkcją. –

Odpowiedz

2

Ponieważ operator ciąg jest szablon, to nie można odebrać, podczas gdy inny operator może.

Powiązane problemy