Parameter Matching


A call to a function selects from all functions that overload the specified function name. The selection process involves considering all functions of the given name (in the current scope) for which conversions exist from the calling parameter list to the function's formal parameter list. The function whose formal parameter list best matches the calling parameter list is selected. The best matching function is defined to be the intersection of the sets of eligible functions that best match on each parameter. If the intersection is empty then the call cannot be matched. The call is ambiguous unless the intersection consists of exactly one function; that is, unless a function can be found that is a better match on a particular parameter than all other functions in the sets being intersected.

The following considerations apply to parameter matching.

The following rules are applied to conversion sequences.

Exact Match

A sequence with zero or more trivial conversions is considered superior to all other conversion sequences. An exact match sequence that does not convert T* to const T* or to volatile T* is superior to one that does. An exact match sequence that does not convert T& to const T& or to volatile T& is superior to one that does.

Match with Promotions

Amongst sequences that are not an exact match, sequences that perform only trivial conversions, integral promotions and conversions from float to double are considered superior to all the rest.

Match with Standard Conversions

Amongst sequences that are not an exact match or a match with promotions, sequences that perform only trivial and standard conversions are considered superior to all the rest. Of matches with standard conversions, sequences with conversions of the form D* to B* (where D derives publicly from B) are considered superior to sequences converting D* to void* or D* to const void*. Also, if C publicly derives from B and B publicly derives from A then a conversion of the form C* to B* is superior to a conversion of the form C* to A*. Similarly, a conversion of the form C& to B& is superior to a conversion of the form C& to A&. Pointer to member conversions are subject to the same considerations with regard to the inheritance hierarchy.

Match with Application Defined Conversions

Amongst sequences that are not an exact match, a match with promotions or a match with standard conversions, sequences that perform only trivial, standard and application defined conversions are considered superior to all the rest.

Match with Ellipsis

Sequences that involve matching with an ellipsis are considered to be inferior to all other conversion sequences.

The following facts also apply to conversions when used in parameter matching.

Examples

The following example demonstrates the selection of application defined conversions.

class test
{
 public:
  operator int();     // Conversion to int
  operator double();  // Conversion to double
  ...
} instance;           // Declare an instance of test.
 
int i = instance;     // Calls test::operator int().
double n = instance;  // Calls test::operator double().
float f = instance;   // Error ! Ambiguity between
                      // test::operator int() and test::operator double()

The next example demonstrates the point that standard conversions may be applied in addition to application defined conversions (in either order).

class test
{
 public:
  test(long);      // Constructor accepting long
 
  operator int();  // Conversion to int
  ...
} instance;        // Declare an instance of test.
 
void f(long);
void g(const test&);

f(instance);       // Same as: f(long((int)instance));
g(0);              // Same as: g(test(long(0)));

The following example demonstrates the point that if an explicit conversion or type cast is required to prevent ambiguity then no account is taken of possible best match sequences of standard conversions that may be implicitly used to obtain a match.

class A
{
 public:
  A(int);     // Constructor accepting int
};
 
class B
{
 public:
  B(long);    // Constructor accepting long
};
 
void f(A);
void f(B);
 
f(1);        // Ambiguous despite the fact that f(B(long(1))
             // requires one more standard conversion than f(A(1)).

The following example illustrates the fact that conversions by constructor are not given precedence over conversion operators and vice-versa.

class A
{
 public:
  operator int();  // conversion operator
  ...
};
 
class B
{
 public:
  B(const A&); // conversion constructor.
  ...
};
 
B operator*(const B&,const B&);

void f(const A& A1, const A& A2)
{
 A1 * A2; // Error: Ambiguous - could be interpreted as either
          //        operator*(B(A1),B(A2)) or as
          //        (int)A1 * (int)A2
}