问题
Why aren't const B
and const A*
indistinguishable, when B
is typedef'ed to A*
? When compiling this simple example:
struct A {};
typedef A* B;
void f1(const A* a1);
void f2(const B a2);
int main()
{
const A a;
f1(&a);
f2(&a);
}
I get the following compiler output (G++ 6.3.1):
test.cpp: In function ‘int main()’:
test.cpp:12:8: error: invalid conversion from ‘const A*’ to ‘B {aka A*}’ [-fpermissive]
f2(&a);
Note that the call to f1
is fine (since &a
is-an A*
), but f2
is not, even though const B
seems to me like it should be equivalent to const A*
.
回答1:
const B
means A * const
that is the pointer itself is a constant but the object pointed to by the pointer is not constant.
On the other hand const A *
means that the pointer itself is not constant but the object pointed to by the pointer is constant.
So const B
that is A * const
is not an alias for const A *
You could write
typedef const A* B;
void f1(const A* a1);
void f2( B a2);
回答2:
This is a case of "leading const" being misleading.
If you have
struct A {};
typedef A* B;
Then a const B
is in fact A * const
. The const-qualifier is applied on the pointer-type, not the pointee-type as it is in const A*
.
Side note, but the reason I favor "cv-qualifiers on the right" is that reading the type name from right to left in English, matches the actual type defined. So for instance:
int const * * const * const p;
When read from right to left becomes "p is a constant pointer, to a constant pointer, to a pointer, to a constant integer".
回答3:
even though
const B
seems to me like it should be equivalent toconst A*
.
No, for const B
, const
qualifies B
, i.e. the pointer itself, not the pointee. Then const B
is equivalent to A* const
, i.e. a const pointer to non-const A
, not a pointer to const A
; which can't be converted from const A*
without explicit conversion.
回答4:
A const A*
is a pointer to a const A
. The pointer is mutable, but the object it points to is not.
A const B
is a const
pointer to an A
. The pointer is immutable, but the object it points to is mutable.
The second function is equivalent to void f2(A* const a2)
, if I recall.
来源:https://stackoverflow.com/questions/44190328/const-b-and-const-a-are-incompatible-even-when-b-is-aliased-to-a