double(?) definition of template member operators
if I look into < complex> header from msvc 2008, I find that each operator definition is doubled with _Other type template operation, like for example
_Myt& operator+=(const _Myt& _Right)
{ // add other complex
this->_Add(_Right);
return (*this);
}
template<class _Other> inline
_Myt& operator+=(const complex<_Other>& _Right)
{ // add other complex
this->_Add(_Right);
return (*this);
}
The question is why would not the second definition alone suffice?
PS: It seems in gcc there is only second definition present, now I do not worry any more. :)开发者_Python百科
The first case also catches a right-hand-side that's convertible to _Myt
.
class MyComplex {
// ...
public:
operator std::complex<double>() const;
operator std::complex<float>() const;
};
std::complex<double> i;
i += MyComplex(1,1); // Unambiguously uses the first form.
精彩评论