The process of choosing the correct function to use from a function call is called Overload Resolution. When a function is called, the compiler searches for all functions with that name (overloads) and compiles them into an overload set. Simply put, a best match is chosen by picking the functions that require the least conversions as possible from their parameters.
These are the two functions compiler chooses from a(c)
:
int a(const int& b);
int a( int& b);
The second overload is chosen because the first overload requires a const
-qualification. The variable with which you called the function with, c
, is non-const
, so it is a perfect match for the second overload and can be bound to the non-const
reference.