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);
}
The compiler selects which overloaded function to invoke based on the best match among the function declarations in the current scope to the arguments supplied in the function call. If a suitable function is found, that function is called. "Suitable" in this context means either: An exact match was found.
When a function name is overloaded with different jobs it is called Function Overloading. In Function Overloading “Function” name should be the same and the arguments should be different. Function overloading can be considered as an example of a polymorphism feature in C++.
Rules of Function Overloading in C++The functions must have the same name. The functions must have different types of parameters. The functions must have a different set of parameters. The functions must have a different sequence of parameters.
This feature is present in most of the Object Oriented Languages such as C++ and Java. But C doesn't support this feature not because of OOP, but rather because the compiler doesn't support it (except you can use _Generic).
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 sequenceS2
if[...]
(3.2.5) -
S1
andS2
differ only in their qualification conversion and yield similar typesT1
andT2
(4.4), respectively, and the cv-qualification signature of typeT1
is a proper subset of the cv-qualification signature of typeT2
.
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 sequenceS2
if[...]
f) Or, if not that,
S1
andS2
only differ in qualification conversion, and the cv-qualification of the result ofS1
is a subset of the cv-qualification of the result ofS2
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With