2012-04-07 22 views
5

Aşağıdaki kodu dikkate alın. Benim GCC derleyicisi örtülü Sınıfım :: operatör dizisi (kullanmaya çalışmaz neden Myclass :: operatör dizisi() tanımlanmış olmasına rağmen ben,) anlamıyorum: string operatörü bir şablonNeden Myclass :: operator string() dosyasını std :: string :: operator +() ile çağırmak gerekiyor?

#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

Nedeni inanıyorum ki std :: operator + 'bir işlev şablonu değil, bir işlev. –

cevap

2

Çünkü o diğer operatör ise alınamaz.

İlgili konular