tags:

views:

83

answers:

1

C++03 $4.10- "The conversion of a null pointer constant to a pointer to 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

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

  2. What is the impliciation of this quote (overload resolution?)? Any examples?

+3  A: 

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.

AndreyT
Now that it is a one step conversion process to 'const int *', then shouldn't it call the 2nd overload instead of first? Why ambiguity?
Chubsdad
I think ambiguity comes because the type of 'o' is int. An implicit conversion is required to convert the integer 0 to the NULL pointer constant 0 to match both the overloads (and hence both are of CONVERSION rank). Does qualitification conversion really come into picture here?
Chubsdad
@Chubsdad: Er.. No. C++ language has no such thing as implicit conversion from `int` to pointer type. The literal `0` in this case is treated in a special way - as a null-pointer constant. Type `int` never comes into play in this case.
AndreyT
@AndreyT: I did not say implicit conversion from int to pointer. I meant implicit conversin from int 0 to NULL pointer constant 0
Chubsdad
@Chubsdad: that's not even a conversion in the C++ standard. (You might be thinking of Integral Constant Expression 0 to Null Pointer Constant, which isn't a conversion. Null Pointer Constants are defined as a subset of Integral Constant Expressions)
MSalters
@MSalters: "A null pointer constant can be converted to a pointer type; the result is the null pointer value of that type and is distinguishable from every other value of pointer to object or pointer to function type."
Chubsdad