How c++ deduce the right overloaded function?

  c++, c++98

There was 5 different versions of pow, in C++98:

double pow (double base, double exponent);
float pow (float base, float exponent);
long double pow (long double base, long double exponent);
double pow (double base, int exponent);
long double pow (long double base, int exponent);

My teacher told me, that before c++11 (before adding template version of pow), there could be an error where c++ can’t deduce which overloading to choose. That seemed reasonable to me. For example consider this code:

#include <iostream>
#include <cmath>
using namespace std;

int main()
{
    cout << pow(4, 3);
    cin.get();
}

I’m calling pow() function with two integer arguments. I don’t have such overloading of pow(), so maybe I should do some implicit conversions? Ok, but which one to choose? I can’t choose how to convert since I have ambiguity in choosing of the overloading. But I tried to compile this piece of code (in std=c++98 mode) and it worked. Why?

Ok, maybe it’s because of second argument being integer. Therefore I have to choose only between double pow (double base, int exponent) and long double pow (long double base, int exponent). But still which one compiler decides to choose? What if I called pow(4, 3ll)? It still compiles but type deducing for me is less obvious.

upd: Maybe it is a bad idea to see how deducing works here, because I may never know how pow really implemented. Or is it?

Source: Windows Questions C++

LEAVE A COMMENT