§4 Function overload and default parameters
":( ..." I suddenly popped up such a bitter gourd on my MSN, and I don't have to look at who.
"Master, come over, is it good?"
"It's not said that I will call me a master! I can call me Weily. What happened? What is wrong?"
"It is not clear, or you ... old man ... come and look at it: P" Young people also deliberately separate the words "old man" and before and after, changed to the black body, three words, but also colored, and Bold.
Fortunately, I just picked up the cup, I haven't given water yet, otherwise, my keyboard and display will be.
"I am angry? I have a joke, you still come over, weily :)"
"My gas is not so small, but the next time you are sending this kind of words, I will see if I have a drink or eat something, I am afraid of being smashed ... well, I will come over!" After the Enter, I took my teacup and walked over.
"What happened? What is wrong?"
"I have some code here that I can't be compiled, I have an error, but I don't know why I am wrong."
"Well, I told you last time, I have to learn the fault of the compiler."
"I saw it, it said: 'Ambiguous call to overloaded function', that is, the call of the overload function has an unsurified. However, I didn't see what is wrong!" Young looks like a face. Innocent and ignorant, like a child who is monster.
"Hey, it is an erriness problem. This problem is indeed when it is more headache, because C has such a philosophical belief: it believes that the potential ambiguity is not a mistake (Note 1). Let me see first Your code. "
"Well, it is here." YOUNG pointed the mouse to the wrong line, "I just want to do it, how can such a simple function call error."
I saw a screen, just a function call, just like this:
Func (i);
"First, I ask you, what is the prototype of this FUNC function?"
"Function prototype? Oh, is the function of the function? Here." She pressed the PageUp button, and then refer to me to me.
Void func (char); void func (long);
"Well, this way, what type is your I this variable?"
"It is int."
"Hey, this is no problem, I know."
"Well? Know what? Say it to me!" Young is staring at me with curious eyes.
"In fact, this is a stateless state that is often overlooked. Do you think that from int change to char, will lose the accuracy, and turn it to long, so you think this function call, should Undoubtedly call FUNC (long) function? "
"Yeah, because I remember that SolmyR Boss said, when the compiler did not know when it was turned into that type, it would report an erroneous error. But here, int change to char and int convert to Long should be obvious , Turned to Char will lose the accuracy, and it should be better to turn to LONG. "
"Indeed, you are basically right, and I think so too."
"In the past? Are you?"
"Yes. But as long as you look at TC Pl, there is such an example in 7.4.3, almost the same as your code, Bjarne clearly pointed out in TC PL, this It is an unisforced function call. However, unfortunately, he has no specific explanation why this call is an unsatisfactory. "" Hey, what is this going? "
"You listen to me slowly. First, when overloading the parsing, the compiler should be selected in accordance with a rule in C . A total of five types of matching and conversion. The highest level is exactly matching. "
"Well, this I know, if there is a function here if there is a function of Void func (int), the call here will not have an unsure, and it is sure to call this function as a parameter in INT."
"Yes. But he also includes a few ancestors to convert into a corresponding pointer type, the function name is converted into a corresponding function pointer, and the very amount of type converted to the corresponding constant type, these are very subtle conversion, the type and the type of conversion The conversion is quite. For example, "
Void f (const char *); char p [] = "non-const"; f (p);
"There are 2 conversions here, one is to convert the ancestors of the PHAR to the corresponding char * type pointer, the second is to convert from char * to const char *, is a very varied conversion to constant, these It is exactly a precise match. "
"Oh, got it."
"The second level is the improvement of the type. In fact, this is involved in the problem you propose. In this type of increase, this design is not converted from int to Long, which is inherited. Since C, it is the size of the calculation object to the corresponding 'natural' before the operation. "
"'Natural' size? What is this?"
"This is very common in C, for example in C, Char is converted to int for processing. About Char and Int relationship, in C, both are equivalent, can be said in C without char This type of value, any char value is int, such as constant 'a', in which it is int, rather than char, so many C's code is hidden from char to INT Type conversion. Of course, in C , it is still distinguished, that is, the type of matching type is presented, and the type of match is used. For example, "
Void F (int); Void f (int);
f ('a');
"Hey, I know, there is no second-meaning here, I will call the function of f (char)." Young seems to have experienced.
"Yes, but this?"
Void f (int); Void F (unsigned char);
f ('a');
"This ..." Young has fallen into confusion.
"The answer is to call F (int), there is no difference here. Remember, that is, those smaller integers, including char, convert to int types, otherwise, the conversion is Unsigned int type (Note 2). This is the 'natural' conversion in front. About this implicit type conversion, you can look at the C.6 of TC PL, where there is a detailed description. "
"Yeah, I got it."
"In fact, the cause of these issues is C inherits a rule in C , which is not distinguishing Narrowing Conversions and Widening Conversions, so it will bring it from int convert to Char, or INT converted to long, this ambiguous two State. If Narrowing Conversions and Widening Conversions are distinguished, you will define implicit Widening Conversions (e.g., from int to long) and explicit Narrowing Conversions (for example, a explicit type conversion operation from INT to Char). But This design is unacceptable because this implicit type conversion is too common like INT to Char, and if it is necessary to become explicit type conversion, then the compatibility of C and C will be greatly reduced. Make C not easy to be used and accepted by people, the design principle of C is not forced to use a way to complete the required things, it will provide choices, let people can use their favorite ways, and C in the original design Time, you have to do things that C can do, then the original C code should be able to be accepted by C . "" So C does not distinguish Narrowing Conversions and Widening Conversions, and the compiler is given to Narrowing Conversions is a warning, Not the error. Is it like this? "
"Yes, a good compiler will give a warning for this possible loss of accuracy. It seems that you have understood most of this problem. So let's take a look at the relationship between integers and pointers. Question, what is 0? "
"0? Is an integer."
"Basically, it is more accurately, 0 is an integer integer constant. For a constant expression of 0 integer, or a constant expression of 0, it can be implicitly converted to any type of pointer. So, for the following code, it is an emphasis function overload. "
Void func (char *); void func (int *);
Func (0); // Erlighter function call!
"Well, it turns out, I know."
"So how much do you know about the default parameter of the function?"
"Default parameters? Oh, this I have seen it, it seems that the most critical is that the default parameter can only be the last few in the form of form parameters."
"It's not seemingly seeming! This must remember it. In fact, the default parameters also have a certain relationship with the overload. Do you see what the following code is there."
Void Func (INT I); Void Func (INT I, CHAR CH = 'A');
INT I; FUNC (I);
"Hey! Ambiguous two can, this function call is unsatisfactory!" Young looks like this year of Columbus discovered the new mainland.
"For! For multi-parameter function overload, the compiler is decided which function is called, or follow the best match rules, which is to make the best match as much as possible. Two, Coupled with standard conversion, user-defined conversion and omitting numbers in function definitions constitute five matching rules, the higher the front level, of course, in these three rules, it also involves user-defined The design problem of class, when you have learned classes and inheritance, we will say. These rules have descriptions in the 7.4 of TC PL, you should look at it. "" Book? I am watching. In fact, today I have encountered, I just saw something about functions of functions, but there were still some unaware. Yesterday you are not, I will ask Pisces, pay attention to what to pay attention to it. As a result, she said that I will give you some code. You go to all the mistakes, and you can learn these mistakes, then you can learn. Finally, I have lost my words, try my best to avoid the ambiguity. status!"
"So just the code is what she gives you?"
"Yeah, and, I can't make it, I will ask her, she didn't say something, I didn't write like this when I wrote the code later."
It turned out that the ghosts made by Pisces, they were indiscriminate, and they were thrown to new Young, and then I will let me solve this problem through Young. If I have not guess, she should now be nearby ... I turned, I saw that she plays the ghost face on the side, and, she looks like a gain, I want to slip away.
"Pisces, you can ask me or more solmyr, why bullied people from yourself?"
"This ... I just see what she is studying in this area, and I see her mixing with you, so let me ..." This guy is so embarrassed.
"Well, this time you don't care, you can't do this next time ..." I haven't finished it, Pisces has returned to my own seat, really! "
Huh? How to get Young is gone? I used the left-hand index finger and the middle point to the glasses on the nose, and then looked around. As a result, I saw a cup on the hands of YOUNG and came here. Is that the cup not mine? How can I be in her?
"Master, please drink tea, this is the favorite black tea for your old man, but also add honey!"
Red tea plus honey? I haven't heard it wrong? I took the cup, and I really smelled the familiar tea fragrance, and the cup was taken in the hand, there is a warm feeling.
"How do you know that I am used to?" I think it is a bit curious, she is only coming soon, how can I know that I like black tea? Like Pisces for so long, I didn't give me a cup of coffee last time.
"This ah? Secret!" Young smiled.
"Well, good! The taste is just! Hey, no, what are you calling me? How can I hear the 'teacher' word, I still hear the 'old' word, you ..." I reacted, and was She is playing ...
Note: 1. This statement comes from the clause of the "Effective C " in Scott Meyers.