const B and const A* are incompatible, even when B is aliased to A*

Viewed 205

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

4 Answers
Related