Doesn't Visual Studio 2008 compiler start automatically with sqrt in C ++?

Should the compiler automatically apply a double value in the following? At least according to Walter Savich.

#include <iostream> #include <cmath> using namespace std; int main(){ int k; for(k = 1; k <= 10; k++) cout << "The square root of k is: " << sqrt(k) << endl; return 0; }//error C2668: 'sqrt' : ambiguous call to overloaded function //Visual Studio 2008 on Win Vista 
+4
source share
6 answers

The problem is that there are three versions of sqrt :

  double sqrt ( double x ); float sqrt ( float x ); long double sqrt ( long double x ); 

Since you pass int , the compiler will advance your argument, but it is equally valid for advancing your integer to any of the above types, so it is ambiguous.

You can fix this by simply specifying one of the above types, for example:

 cout << "The square root of k is: " << sqrt((double)k) << endl; 
+8
source

An ambiguous call error is that it does not know which function to call not what it implicitly converts.

look at the following. If I create my own function that takes a double and returns double, it has no problem that is implicitly converted. Since your integer can be converted to any of the three overloads, it does not know what to call.

 double mysqrt(double d) { return d; } using namespace std; int main(int argc, char ** argv) { int k; for(k = 1; k <= 10; k++) cout << "The square root of k is: " << mysqrt(k) << endl; return 0; }//Works Fine 

However, if I add another version of mysqrt that accepts a float, I create an ambiguous call error.

 double mysqrt(float f) { return f; } double mysqrt(double d) { return d; } using namespace std; int main(int argc, char ** argv) { int k; for(k = 1; k <= 10; k++) cout << "The square root of k is: " << mysqrt(k) << endl; return 0; }//error C2668: 'mysqrt' : ambiguous call to overloaded function 
+4
source

Since there are several sqrt (overload) functions , and more than 1 can accept int as a parameter without loss of accuracy, it is up to you specify which (and this is good, because you do not want the compiler to make decisions for you based on "indirect" evidence - passing int to sqrt).

+2
source

C always returns a double version if sqrtf is not called.

Try to make your call with (double)

From MSDN: [

 double sqrt( double x ); float sqrt( float x ); // C++ only long double sqrt( long double x ); // C++ only float sqrtf( float x ); 

] 1

0
source

There are several overloads in std :: sqrt in cmath. One takes a float and the other takes a double. The compiler cannot know what you want to be called, and ints can be launched in any of them.

In the absence of additional overloads, it will automatically reset double [this is the case with C], as you expect.

0
source

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.

-1
source

Source: https://habr.com/ru/post/1304228/


All Articles