all*_*llo 3 c++ implicit-conversion ceres-solver
当我有一个数字类型,它定义operator<为double,但不是int,与int literals的比较不起作用.这是一个问题,作为标准库的一部分,即std::complex包含int文字.
在使用类型时,我可以使编译器将int文字视为double吗?
简化示例:
// the defined operator
template<typename T>
bool operator<(const Type<T> &lhs, const T &rhs);
complex<Type<T>> complex_number;
1.0 / complex_number; // this fails
Run Code Online (Sandbox Code Playgroud)
失败发生在at 的_Div方法中std::complex
template<class _Other> inline
void _Div(const complex<_Other>& _Right)
// ...
else if ((_Rightimag < 0 ? -_Rightimag : +_Rightimag)
Run Code Online (Sandbox Code Playgroud)
这会导致错误:
error C2678: binary '<': no operator found which takes a left-hand operand of type 'Type<T>' (or there is no acceptable conversion)
(...)
complex(665): note: while trying to match the argument list '(Type<T>, int)'
[
T=double
]
Run Code Online (Sandbox Code Playgroud)
我想可能代码std::complex应该是_Rightimag < static_cast<_Other>(0)使用所有数字类型,但我必须使用stdlib提供的内容.
由于另一种类型也来自库,我正在寻找一种方法来将隐式转换添加到我的代码中.
对于实际代码:我正在使用ceres,它允许您使用模板化标量类型定义仿函数以进行自动分化.标量都被评估为T和Jet<T, N>.
Ceres 定义 operator<(const Jet<T, N>&, const T&),允许jet < 0.0但不允许jet < 0.
在我的代码,我可以解决此问题通过双打或明确地铸造整数模板类型T,但是当我尝试的工作与complex<T>我惹上麻烦了其中比较反对整型常量,就像方法_Div上面的方法.
该std::complex模板不要求与一般类型的工作.标准说[complex.numbers]/2:
实例化模板的效果
complex比其他任何类型的float,double或者long double是未指定的.
如果您需要将任何其他类似数字的类型概括为类似复杂的类型,那么您应该使用一些不同的库或实现自己的库.