1

Is there anyway in C++11 to do this:

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

The error is

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

I have tried static_cast like;

std::complex<double> answer = static_cast<std::complex<double>>(integer)*doub;
1
  • Note that the Standard does not require std::complex<int16_t> to work in any meaningful way (only the three floating types have defined behaviour)
    – M.M
    Jul 5, 2018 at 8:10

1 Answer 1

1

There's no predefined convertion from complex<double> to complex<int16_t> or viceversa.

You can define your own:

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.