The question raised a few days ago was not stated clearly. Now, I will try to raise this question in the shortest possible language.
Just look at an example here directly, or just look at the key clips I intercepted directly:
A pointer to a non-const type can be implicitly converted to a pointer to const-qualified version of the same or compatible type:
int* p = 0;
const int* cp = p; // OK: adds qualifiers (int to const int)
My point is that there is a bit of a problem with this example. (My point may not be correct.)
Next, I will introduce my understanding in detail(Similarly, it may not be correct):
Let’s first have a look at the conversion from floating-point numbers to integers and give a perfect example for it
(The implicit conversion from floating-point numbers to integers needs to rely on other implicit conversions because it is impossible for a floating-point number to appear anywhere and be immediately implicitly converted to an integer. This is absurd.
Therefore, we can rely on such an implicit conversion, In simple assignment (=), the value of the right operand is converted to the type of the assignment expression(C11 6.5.16.1 p2):)
int n=3.14; //double to int
Because there is an implicit conversion required by simple assignment here, the implicit conversion from floating-point numbers to integers occurred.(Although this is initialization, based on what I have learned, I can only think that the implicit conversion occurred because of the simple assignment.)
It is the same for implicit qualification conversion.But it should be noted that “The type of an assignment expression is the type the left operand would have after lvalue conversion.(6.5.16 p3)”Meanwhile, this point has already been mentioned when evaluating the constraints of the assignment:
— the left operand has atomic, qualified, or unqualified pointer type, and (considering
the type the left operand would have after lvalue conversion) both operands are
pointers to qualified or unqualified versions of compatible types···(6.5.16.1 Constraints p1)
Then in the first example, p
has the type int*
, which is correct. But the type of the assignment expression should also be int*
instead of const int*
(After the virtual lvalue conversion), so why can the first example be used as an example of implicit qualification conversion?
So, my question is, is my above analysis correct? If it is correct, please give a perfect example that conforms to the implicit qualified conversion. If it is wrong, please point it out in detail.
Thank you for reading it. To be honest, I’m not confident that I’m right. I’d be happy for you to point out my mistakes.