c++ - why type casting doesn't work in here? -
the following code has type casting error
#define img_i (std::complex<double>(0, 1)) #define pi 3.1415926535 (unsigned long int j = 0; j < 10; ++j) std::cout << exp(-img_i * pi * j);
the type casting can solved using parenthesis or changing order of multiplication. not clear me why type casting problem occurs in first place , why c++ cannot handle above code is.
can explain me?
this operator*
overload std::complex
function template declaration looks like:
template<typename t> complex<t> operator*(const complex<t>& lhs, const t& val);
the t
in complex<t>
lhs
argument must same t
val
argument in order template argument deduction work.
you trying call std::complex<double>
(the type of -img_i * pi
) , unsigned long
(the type of j
). double
, unsigned long
not same type, template argument deduction fails.
-img_i * (pi * j)
works because type of pi * j
double
, there no ambiguity t
is. likewise, -img_i * pi * static_cast<double>(j)
works same reason.
Comments
Post a Comment