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 reported ambiguous even with -pedantic -std=c++98 -Wall -Wextra
, but it doesn't necessarily work at all in other compilers, for the same reason foo(i)
doesn't.
gcc adds the following to namespace 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 X.
I appreciate g++ doing its best to help me out and all, but is there any (legitimate) way to make it diagnose the bug in my code?
[Edit: I'm using gcc 4.3.4, but if other versions of gcc can diagnose it then I'm interested in that fact too!]
sqrt
overloads to namespace std, although I haven't checked the standard for a reference. I'm not trying to claim that gcc is buggy, just that I'd prefer it to behave differently if possible. – Wraithstd::sqrt
, but using the existing tag is definitely better. I should watch out for that, the power to create tags means I did so entirely by accident... – Wraith