I think this question is not new, but I can not find an answer anywhere.
Is there a difference between (double)myInt
and double(myInt)
in c++?
I think this question is not new, but I can not find an answer anywhere.
Is there a difference between (double)myInt
and double(myInt)
in c++?
Is there a difference between (double)myInt and double(myInt) in c++?
Only difference is syntactical. The meaning is the same.
The latter "functional" syntax cannot be used with compound types: int*(x)
does not work.
(double)myInt
is type conversion from int
to double
. In modern C++, it is strongly advised to use static_cast<double>(myInt)
.
double(myInt)
calls double
's "constructor" which also does the type conversion.
Essentially, they are the same and will result in the same ASM output.
Check the following code:
#include <iostream>
int main() {
int a = 5;
double b(a); // line 1
double c = (double)a; // line 2
return 0;
}
Both lines result with the same ASM:
cvtsi2sd xmm0, DWORD PTR [rbp-4]
movsd QWORD PTR [rbp-24], xmm0
Check here