I cannot initialize a non-const reference to type T1 from a convertible type T2. However, I can with a const reference.
long l;
const long long &const_ref = l; // fine
long long &ref = l; // error: invalid initialization of reference of
// type 'long long int&' from expression of type
// 'long int'
Most problems I encountered were related to r-values that cannot be assigned to a non-const reference. This is not the case here — can someone explain? Thanks.
An integer promotion results in an rvalue.
longcan be promoted to along long, and then it gets bound to a const reference. Just as if you had done:Contrarily an rvalue, as you know, cannot be bound to a non-const reference. (After all, there is no actual
long longto refer to.)