const int *: The pointer itself is mutable, but the value pointed to is immutable
int * const: The pointer itself is immutable, the value pointed to is variable
Converting const int * -> int * const will report an error
Conversion int * const -> const int * can be done
Semantically speaking, it is easier to understand, but from a formal point of view, both have an immutable quantity. So why is one direction feasible during conversion, but not the other? After thinking about it, it may be becausepointers and values are not on the same level, but I don’t know how to make it clear?
Assume that what is discussed here is implicit conversion (similar to static_cast):
The underlying const complies with qualification conversions: the target type must be more qualified. That is, const can only be more but not less.
The top-level const rules are more complicated. For class types, whether they can be converted depends on the conversion constructor and conversion function. For non-class types, there is no such thing as top-level const conversion. Clause 4 standard conversions say nothing about top-level const.
Here we discuss several situations involving top-level const:
Expression:
Assignment:
Initialization:
That is to say, when what is required is prvalue, there is no const prvalue. Regarding glvalue, there are the following conventions:
This paragraph stipulates the value category after implicit conversion. When the result of implicit conversion is prvalue, there is no const; and when the result is glvalue, the target type of conversion must be lvalue reference or rvalue reference. In this case, if the following initialization holds, it can be converted:
T &t = e;
T &&t = e;
const T &t = e;
const T &&t = e;
reference-compatible restricts whether it can be initialized. The rules here are similar to the rules of qualification conversion. In addition, these consts are already underlying consts.
PS: There is a footnote at qualification conversions: "These rules ensure that const-safety is preserved by the conversion.". All implicit const conversions, whether agreed upon or imaginary, are fine as long as they can ensure the safety of const. If a const conversion ensures the safety of const, but cannot be implemented because it violates some other terms, then this is probably a language pill. Funny