Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c++ function overloading with Exact Match

Tags:

c++

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);
}
like image 691
sniel Avatar asked Aug 12 '16 21:08

sniel


People also ask

How is matching done in case of overloaded functions?

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.

What is function overloading write the steps to find unique match during compilation?

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++.

What are the rules for function overloading?

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.

Why overloading is not possible in C?

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).


1 Answers

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

like image 173
user657267 Avatar answered Oct 06 '22 22:10

user657267