I have a question regarding C ++ function matching for type parameters T and const T& . Let's say I have the following two functions:
void f(int i) {} void f(const int &ri) {}
If I call f with an argument of type const int , then this call is, of course, ambiguous. But why is calling f with an argument of type int also ambiguous? Wouldn't the first version of f be an exact match, and the second would be worse because the int argument must be converted to const int ?
const int ci = 0; int i = 0; f(ci);
I know that such an overload does not make much sense, because calls to f almost always ambiguous, unless the parameter type T has an available copy constructor. But I'm just learning the rules for matching functions.
Regards, Kevin
EDIT: Make my question clearer. If I have two functions:
void f(int *pi) {} void f(const int *pi) {}
Then the following call is not ambiguous:
int i = 0; f(&i); // not ambiguous, first version f(int*) chosen
Although both versions of f can be called using &i , the first version is selected because the second version of f will include the conversion to const. That is, the first version is “the best match.” "But in two functions:
void f(int i) {} and void f(const int &ri) {}
This extra conversion to const ignored for some reason. Again, both versions of f can be invoked with int . But then again, the second version of f will require conversion to const , which would make a worse match than the first version of f(int).
int i = 1; // f(int) requires no conversion // f(const int &) does require a const conversion // so why are both versions treated as "equally good" matches? // isnt this analogous to the f(int*) and f(const int*) example? f(i); // why ambiguous this time?