C++03 $4.10- "The conversion of a null pointer constant to a pointer to 开发者_Go百科cv-qualified type is a single conversion, and not the sequence of a pointer conversion followed by a qualification conversion (4.4)."
Here is my understanding
int main(){
char buf[] = "Hello";
char const *p1 = buf; // 2 step conversion process
// C1: char [6] to char *
// C2: char * to char const * (qualification conversion)
char const *p2 = 0; // $4.10 applies here
}
Is my understanding (as in the code comments) correct?
My question is
What is so significant about the quoted portion of $4.10 that it deserves a mention? Not that it hurts to be there, but then I don't understand it I think.
What is the impliciation of this quote (overload resolution?)? Any examples?
Your understanding is correct.
And the answer to both of your questions is indeed overload resolution. Overload resolution has to compare different conversion sequences in order to find the best one and thus select the best viable function. And when it comes to comparing standard conversion sequences, one of the rules (described in 13.3.3.2/3) is that if one sequence is a proper subsequence of the other, then the shorter sequence is better than the longer one.
For example, if "null-pointer-constant to cq-qualified null-pointer-value" conversion was a two step process, then this conversion would be considered worse than "null-pointer-constant to non-cq-qualified null-pointer-value" conversion in accordance with rule mentioned above. This would look illogical, at least to me. I prefer to see this code fail
void foo(int *);
void foo(const int *);
...
foo(0);
due to ambiguity instead of quietly resolving to foo(int *)
. And it does fail, as required by the specification.
精彩评论