2012-04-07 5 views
5

以下のコードを検討してください。私のGCCコンパイラが暗黙的にMyClassのを使用しようとしない理由を私は理解していない::演算子文字列を()、MyClassの::演算子文字列()が定義されているものの:std :: string :: operator +()で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) 
    */ 
} 
+2

私は理由が 'std :: operator +'は関数テンプレートではなく、関数テンプレートであると信じています。 –

答えて

2

文字列演算子がテンプレートであるので、それを他のオペレータが行うことができますが、ピックアップすることはできません。

関連する問題