将complex <int16_t>转换为complex <double>

时间:2015-12-11 08:59:23

标签: c++11 casting complex-numbers

C ++ 11中是否还有这样做:

 std::complex<int16_t> integer(42,42);
 std::complex<double> doub(25.5,25.5);
 std::complex<double> answer = integer*doub;

错误是

error: no match for ‘operator*’ (operand types are    
‘std::complex<short     int>’ and ‘std::complex<double>’)
std::complex<double> answer = integer*doub;

我试过像static;

std::complex<double> answer = static_cast<std::complex<double>>(integer)*doub;

1 个答案:

答案 0 :(得分:1)

没有从complex<double>complex<int16_t>或反之亦然的预定义转换。

您可以定义自己的:

template <typename D, typename S> std::complex<D> cast(const std::complex<S> s)
{
    return std::complex<D>(s.real(), s.imag());
}

int main()
{
    std::complex<int16_t> integer(42, 42);
    std::complex<double> doub(25.5, 25.5);
    std::complex<double> answer = cast<double, int16_t>(integer)*doub;
}