With the 4.2.1 g++ compiler I get the following error:
functions.cpp:24: error: call of overloaded ‘pow(long int&, long int&)’ is ambiguous
/usr/include/architecture/i386/math.h:343: note: candidates are: double pow(double, double)
/usr/include/c++/4.2.1/cmath:373: note: long double std::pow(long double, int)
/usr/include/c++/4.2.1/cmath:369: note: float std::pow(float, int)
/usr/include/c++/4.2.1/cmath:365: note: double std::pow(double, int)
/usr/include/c++/4.2.1/cmath:361: note: long double std::pow(long double, long double)
/usr/include/c++/4.2.1/cmath:357: note: float std::pow(float, float)
Here’s the responsible code:
long power(long a, long b) {
if (b < 0) return 0;
return pow(a,b);
}
However, on my 4.6.1 version there are no errors nor warnings for my code where I use two longs for the pow evaluation (even with the -Wextra flag).
Why is this? And is using two longs for the pow function a mistake on my part?
The problem is that there is no definition of pow that takes two longs as parameters. Because of this, the longs will be converted into some other data type, but this conversion causes ambiguity since there are many possible ways that the conversion can happen. gcc doesn’t know how to interpret the call, so it gives you an error.