I have the following code that will not compile, complaining that += operator does not exist. += operator is here declared outside of class A.
template < typename _T >
class A {
public:
operator _T () const { return 42 ; }
};
template <typename _T >
A< _T > & operator += ( A< _T > & l, _T r ) { return l ; }
int main() {
A< int > e, f ;
e += f ;
return 0 ;
}
However, if I implement the operator inside class A, code compiles and works :
template < typename _T >
class A {
public:
operator _T () const { return 42 ; }
A< _T > & operator += ( _T r ) { return *this ; }
};
int main() {
A< int > e, f ;
e += f ;
return 0 ;
}
What is the difference between these two codes? Aren't they supposed to be equivalent?
This was compiled with gcc 4.4.7-4.