How to diagnose sqrt (int &) ambiguous call in g ++ 4.3.4

My code is as follows:

#include <cmath> #include <iostream> float foo(float f) { std::cout << "float\n"; return f; } double foo(double d) { std::cout << "double\n"; return d; } int main() { int i = 16; // foo(i); // ambiguous call, of course return (int) std::sqrt(i); } 

The call in the last line is not ambiguous even with -pedantic -std=c++98 -Wall -Wextra , but it does not necessarily work at all in other compilers, for the same reason foo(i) does not.

gcc adds the following to the std :

 template<typename _Tp> inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value, double>::__type sqrt(_Tp __x) { return __builtin_sqrt(__x); } 

That is, it adds inline double sqrt(X) for all integer types of X.

I appreciate that g ++ does everything it can to help me and everyone, but is there a (legal) way to make it diagnose an error in my code?

[Edit: I am using gcc 4.3.4, but if other versions of gcc can diagnose it, then I am also interested in this fact!]

+8
c ++ gcc overloading sqrt
source share
2 answers

This “useful” addition to the GCC standard library is inappropriate in C ++ 03, according to
[Lib.global.functions] / 2:

The call to the global function signature [described in the definition of the standard library] behaves as if the implementation did not declare any additional global function signatures.

This means that implementations (gcc) are not allowed to add additional overloads (useful or not) if they affect the observed behavior of the program.

+2
source share

Do you need a quote from FDIS?

Last paragraph 26.8:

In addition, there must be additional overloads sufficient to ensure:

  • If any argument corresponding to the double parameter is of type long double , then all arguments corresponding to the double parameters are effectively displayed in long double .

  • Otherwise, if any argument matching the double parameter is of type double or an integer type, then all arguments matching the parameters of double are effectively passed to double .

  • Otherwise, all arguments matching the double parameters are effectively displayed in the float .

In this case, it must be point 2, which applies.

+2
source share

All Articles