gpt4 book ai didi

c++ - double(?) 模板成员运算符的定义

转载 作者:塔克拉玛干 更新时间:2023-11-03 02:05:14 27 4
gpt4 key购买 nike

如果我查看 msvc 2008 中的 header ,我发现每个运算符定义都与 _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);
}

问题是为什么仅第二个定义还不够?

附言:在 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.

关于c++ - double(?) 模板成员运算符的定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7726788/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com