Heavy-load parameter conversion

Source: Internet
Author: User

Extern void ff (INT );
Extern void ff (long, Int = 0 );
Int main (){
Ff (2L); // match ff (long, 0 );
Ff (0, 0); // match ff (long, INT );
Ff (0); // match ff (INT );
Ff (3.14); // error: 2yi
}

==================

Extern int ff (char *, INT );
Extern int ff (INT, INT );
Int main (){
Ff (0, 'A'); // ff (INT, INT)
Return 0;
}
The ff () function of two int-type parameters is selected as the best feasible function for the following reasons:
1. Its first real parameter is better. 0 is the exact match of int type parameters. For the first ff (char *, INT) Function
It requires a pointer standard conversion sequence to match the char * type parameters.
2. Their second real parameters are the same. The type of real parameter A is the matching of the second parameters of the two functions of char.
Both require a conversion sequence for the escalation level

 

Here is another example.
Int compute (const Int &, short );
Int compute (Int &, double );
Extern int iobj;
Int main (){

Compute (iobj, 'C'); // compute (Int &, double)
Return 0;
}
The two functions compute (const Int &, short) and compute (Int &, double) are both feasible functions.
Because the second function is selected as the best feasible function
1. Its first real parameter is better. The reference initialization of the first feasible function is poor because it adds a const
The const modifier is not added to the initialization of the second viable function.
2. Their second real parameters are the same. The type of the real parameter C is that char must match the second parameter of the two functions.
A standard conversion sequence is required.

 

# Include <vector>
Void manip (vector <int> &);
Void manip (const vector <int> &);
Vector <int> F ();
Extern vector <int> VEC;
Int main (){
Manip (VEC); // select manip (vector <int> &) is selected
Manip (f (); // select manip (const vector <int> &) is selected
Return 0;
}
In the first call, both call references are exactly matched during initialization, but this call is not ambiguous because
Both references are initialized with the same const modifier except the second one, so there is no additional limitation on the modifier initialization.
Therefore, manip (vector <int> &) is the first feasible function to be called.
In the second call, this call only has one feasible function manip (const vector <int> &) because the real parameter is saved.
The temporary unit returned by the put function f (). Therefore, this real parameter is a right value and cannot be used for initialization.
The non-const reference parameter of manip (vector <int> &). Therefore, the best feasible function compiler for the second call only
Consider a feasible function manip (const vector <int> &)

 

1. First, it is a conversion from the left value to the right value. It extracts values from the real parameters I and j.
2. Convert real parameters to corresponding parameters through a standard conversion
Because each conversion sequence is as good as the other, the call is ambiguous.
Int I, J;
Extern long calc (Long, long );
Extern double calc (double, double );
Void JJ (){
// Error: ambiguity, no Optimal Match
Calc (I, j );
}

================

Type conversion
The following levels are precisely matched, better than improved, better than standard conversion.

 

Namespace libs_r_us {
Int max (INT, INT );
Double max (double, double );
}
// Using Declaration
Using libs_r_us: Max;
Void func ()
{
Char C1, C2;
Max (C1, C2); // call libs_r_us: max (INT, INT)
}
In the max () call, the real parameters are converted on the real parameters of the char-type call function libs_r_us: max (INT, INT ).
The sequence is as follows:
1A because the real parameter is passed by value, the first step is to convert the value from the left to the right. It extracts the value from the real parameters C1 and C2.
2a incremental conversion converts real parameters from Char to int
The conversion sequence on the real parameters that call the libs_r_us: max (double, double) function is as follows:
1B first applies a conversion from the left value to the right value. It extracts values from the real parameters C1 and C2.
2b floating point -- standard Integer Conversion converts real parameters from Char to double
The level of the first conversion sequence is to improve the worst conversion in the sequence, while the level of the second conversion sequence is standard.
The libs_r_us: max (INT, INT) function is selected as the best feasible function for this call because the upgrade is better than the standard conversion.
Number or optimal matching function

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.