I have a class template parametrized by one typename. I also want to overload its operator= for common case and for double in particular. So I write this code:
Code:
template <typename T>
class field {
field() {}
~field() {}
public:
T val;
field<double>& operator =(field<double>& a);
field<T>& operator =(field<T>& a);
};
template<typename T>
field<T>& field<T>::operator =(field<T>& a)
{
// common code
return a;
}
template<>
field<double>& field<double>::operator =(field<double>& a)
{
// double-specific code
return a;
}
But g++ says it can not compile that:
Quote:
test.cpp: In instantiation of ‘field<double>’:
test.cpp:23: instantiated from here
test.cpp:17: error: ‘field<T>& field<T>:perator=(field<T>&) [with T = double]’ cannot be overloaded
test.cpp:12: error: with ‘field<double>& field<T>:perator=(field<double>&) [with T = double]’
|
What should I do? I want to be able to run specific code for field<double>:
perator=.