Yes, it should automatically cast from int to double. g ++ compiles this fine, so I think VS is wrong
EDIT: not sure why mo answer is rejected. But I think that during function overloading in C ++, automatic conversion should always happen with the highest data type during automatic conversion. for example: if it is possible to convert from to to long, float and double, it should always do int for double conversion.
I tried this in g ++ and it calls the sqrt (double) function.
#include <iostream> #include <cmath> #include <typeinfo> using namespace std; int main(){ int k; for(k = 1; k <= 10; k++) cout << "The square root of k is: " << sqrt(k) <<' '<< typeid(sqrt(k)).name()<< endl; return 0; }
Thus, the output of typeid () is "d", which means that it performed an automatic conversion from int to double.
source share