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!]
c ++ gcc overloading sqrt
Steve jessop
source share