双(?)定义模板成员操作符

double(?) definition of template member operators

本文关键字:成员 操作符 定义      更新时间:2023-10-16

如果我查看<在msvc _x0032_008的complex=">头文件中,我发现每个操作符定义都与_Other类型的模板操作加倍,例如

_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);
}

问题是为什么第二个定义单独不够?

PS:在gcc中似乎只有第二个定义,现在我不再担心了。:)

第一种情况还捕获了可转换为 _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.