sniel
sniel

Reputation: 189

c++ function overloading with Exact Match

Can someone explain why the overloading resolution will pick the 2nd function, instead of reporting an ambiguous error. Anyway, the 2nd function is an exact match, but the 1st undergoes a qualification conversion(from char* to const char*). However, the document from http://en.cppreference.com/w/cpp/language/overload_resolution
states that qualification conversion is also an exact match, and hence both candidates should have same ranking.

Each type of standard conversion sequence is assigned one of three ranks:

1) Exact match: no conversion required, lvalue-to-rvalue conversion, qualification conversion, function pointer conversion, (since C++17) user-defined conversion of class type to the same class

2) Promotion: integral promotion, floating-point promotion

3) Conversion: integral conversion, floating-point conversion, floating-integral conversion, pointer conversion, pointer-to-member conversion, boolean conversion, user-defined conversion of a derived class to its base

void g(const char* x)
{
    std::cout << "g(const char* x)" << std::endl;
}

void g(char* x)
{
    std::cout << "g(char* x)" << std::endl;
}

void callg()
{
    char a[] = "sample";
    g(a);
}

Upvotes: 9

Views: 745

Answers (2)

rahnema1
rahnema1

Reputation: 15867

In overload resolution T* and const T* are considered distinct parameter types. [over.load] 13.1/(3.4):

In particular, for any type T, "pointer to T", "pointer to const T", and "pointer to volatile T" are considered distinct parameter types, as are "reference to T", "reference to const T", and "reference to volatile T".

Upvotes: 0

user657267
user657267

Reputation: 21040

Both functions require an array-to-pointer conversion, but the first requires an additional qualification conversion.

You're correct in saying that both are an Exact Match

[over.ics.scs] / 3

[...] The rank of a conversion sequence is determined by considering the rank of each conversion in the sequence and the rank of any reference binding (13.3.3.1.4). If any of those has Conversion rank, the sequence has Conversion rank; otherwise, if any of those has Promotion rank, the sequence has Promotion rank; otherwise, the sequence has Exact Match rank.

but according to the rules in [over.ics.rank] / 3.2

Standard conversion sequence S1 is a better conversion sequence than standard conversion sequence S2 if

[...]

(3.2.5) - S1 and S2 differ only in their qualification conversion and yield similar types T1 and T2 (4.4), respectively, and the cv-qualification signature of type T1 is a proper subset of the cv-qualification signature of type T2.

The same rule is on the page you linked under "Ranking of implicit conversion sequences"

3) A standard conversion sequence S1 is better than a standard conversion sequence S2 if

[...]

f) Or, if not that, S1 and S2 only differ in qualification conversion, and the cv-qualification of the result of S1 is a subset of the cv-qualification of the result of S2

Upvotes: 8

Related Questions