long long
is not necessarily sized equal to long
and may even use an entire different internal representation. Therefor you cannot bind a non-const reference to long
to an object of type long long
or the other way around. The Standard forbids it, and your compiler is correct to not allow it.
You can wonder the same way about the following code snippet:
long a = 0;
long long b = 0;
a = b; // works!
long *pa = 0;
long long *pb = pa;
The last initialization won't work. Just because a type is convertible to another one, doesn't mean another type that compounds one of them is convertible to a third type that compounds the other one. Likewise, for the following case
struct A { long i; };
struct B { long long i; };
A a;
B b = a; // fail!
In this case A
and B
each compound the type long
and long long
respectively, much like long&
and long long&
compound these types. However they won't be convertible into each other just because of that fact. Other rules happen to apply.
If the reference is to const, a temporary object is created that has the correct type, and the reference is then bound to that object.